Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access the default webdriver for Selenium 2 with Rails 3 Rspec and Capybara?

I have been trying to implement Selenium webdriver for a couple days to do my javascript testing. I have installed and included the gem selenium-webdriver in my Gemfile. A few simple tests pass by adding , js: true to an Rspec test.

Nothing else was necessary: When the tests are run, a Firefox window opens the current page specified earlier in the file with Capybara visit path("/news/#{news_item.id}") and then a button is clicked, displaying a form that was hidden, fields are filled, and a submit button is pressed, no problem. The test passes and the browser closes.

Advancing to more complex tests require me to call methods on the webdriver, but I don't know how to access it, because I did not explicitly create one. I could, however, if I wanted create my own with driver = Selenium::WebDriver.for :firefox but this causes a second browser instance to open, and it is completely blank rather than opening the page Capybara navigated to.

My question is: How can I get access to the default webdriver being used so that I can call methods such as empty_stars = driver.find_element(:class, "empty-stars-container") and driver.action.move_to(empty_stars).perform ?

like image 603
dudeitsdevin90 Avatar asked Jul 31 '12 15:07

dudeitsdevin90


People also ask

What is Rspec capybara?

Capybara is a web-based test automation software that simulates scenarios for user stories and automates web application testing for behavior-driven software development. It is written in the Ruby programming language.

What is header in Selenium?

It is a wrapper around requestly extension to enable easy integration in selenium-webdriver.The extension can modify headers.


1 Answers

You can get access to the webdriver in Capybara using:

page.driver.browser

So you would want to do something like:

empty_stars = page.driver.browser.find_element(:class, "empty-stars-container")
like image 197
Justin Ko Avatar answered Sep 20 '22 02:09

Justin Ko