How to use capybara with watir / watir-webdriver - ruby

I just learned to use selenium with capaybara by setting
Capybara.default_driver = :selenium
But when I changed default_driver to watir-webdriver or watir, I am getting
no driver called :Watir was found

I believe the available drivers are only :rack_test and :selenium at the moment.

Related

How do I open a chrome browser using ruby, cucumber, capybara and bddfire gem?

I am very new at BDD testing and I am trying to figure out how to open a chrome browser in a specific URL using these technologies. I already installed bddfire gem and already ran it.
Now I have a feature:
#openingChrome
Scenario: Opening Chrome on facebook page
Given I open chrome and write "url"
bddfir_steps.rb
Given(/^I open chrome and write "([^"]*)"$/) do |arg1|
$session.visit("https://www.facebook.com.br")
end
In the hooks file I wrote
Before do
$session = Capybara::Session.new(:selenium, browser: :chrome)
end
And in the env.rb file that came with bddfire gem there is this
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app, browser: :chrome)
end
So chrome is already added and I already installed chrome webdriver. Unfortunately this line of code is not working: $session = Capybara::Session.new(:selenium, browser: :chrome)
it throws this error: The second parameter to Session::new should be a rack app if passed. (TypeError)
does anyone know why?
Session#new takes the name of the driver and optionally a rack app instance to start. Since you don't seem to be starting an app and you're registering your driver with a name of :chrome you want
Before do
$session = Capybara::Session.new(:chrome)
end

Disable Javascript for Chrome using Capybara Selenium

I'm wondering if there is a way to disable Javascript for some tests using Selenium-webdriver in Chrome with Capybara.
I have the following:
Capybara.register_driver :selenium do |app|
opts = Selenium::WebDriver::Chrome::Options.new
opts.add_argument '--start-maximized'
opts.add_argument '--disable-infobars'
opts.add_argument '--disable-features'
Capybara::Selenium::Driver.new(app, browser: :chrome, options: opts)
end
Capybara.default_driver = :selenium
But I don't think that --disable-javascript works now.
I've tried to find an alternative but to no joy.
Would anyone have the solution?
I think it is not possible to disable js for selenium-webdriver, however, you can just use :rack_test. driver instead.
From Capybara documentation:
By default, Capybara uses the :rack_test driver, which is fast but
limited: it does not support JavaScript,
So in your case it can be:
it "test description", driver: :rack_test do
...
end

How to run TC in headless (Poltergeist) mode using Ruby, Rspec, Capybara and Selenium?

The problem is that I've tried a lot of variants to declare Capybara Poltergeist driver. But alway get some problems.
For instance, when I declare driver as following:
Capybara::Poltergeist::Driver.new({
js_errors: true,
inspector: true,
phantomjs_options: ['--load-images=no', '--ignore-ssl-errors=yes'],
timeout: 120
})
TC are ran in FF browser instead of poltergeist(((
or, if I declare driver as following:
Capybara.default_driver = :poltergeist
Capybara.current_driver = :poltergeist
Capybara.javascript_driver = :poltergeist
I see such error:
C:/Ruby22-x64/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:121:in `require': cannot load such file -- support/controllers_helpers (LoadError).
Also I need to use Selenium and Capybara methods, therefore I declared following variable to be able to use selenium methods:
#driver = Capybara.current_session.driver.browser
So, the question is how to declare Capybara headless driver (with debugging opportunity if possible) with the opportunity to use Selenium methods (my #driver variable)? enter code here
You can't use the poltergeist driver and use selenium methods -- The selenium specific methods are only available when using the selenium driver.
For your issue of declaring a driver: Declaring/Configuring a driver (using Capybara.register_driver) just names the driver - then you still have to specify to use it (via the name you specified in register_driver) with Capybara.default_driver, current_driver, or javascript_driver - depending on exactly what you're setting up.
I think you really need to think through what you're trying to do and then explain it to us in more detail.

How to access Selenium methods if I defined Capybara Poltergeist driver (for Ruby)

I use "Capybara.current_session.driver" to access Selenium methods when I defined driver as follow:
Capybara.default_driver = :selenium
But when I run my TC (Capybara + Selenium) in headless mode I see that TC are failed due to undefined Selenium methods.
So question is how to access Selenium methods if I defined Capybara Poltergeist driver as follow:
require 'capybara/poltergeist'
Capybara.default_driver = :poltergeist
Capybara.javascript_driver = :poltergeist
Capybara.current_driver = :poltergeist
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app,
:js_errors => false,
:phantomjs_options => ['--ignore-ssl-errors=yes'],
:phantomjs_logger => File.open("F:/Programming/VLoop/Project/28.05.2016/webapp/log/test_phantomjs.log", 'w+')
)
end
You can't use selenium methods when you're using the poltergeist driver, they are only available when your session is using the selenium driver. Thats why it's not recommended to call specific methods directly on the drivers, and instead use the API provided by Capybara so that your tests remain compatible with multiple drivers. What selenium specific methods are you trying to use?

Using Capybara and Selenium test suite fail switching from Firefox to Chrome

Using capybara (2.4.4) to test a non Rails application. I have write some tests, I run the tests using selenium with default firefox web browser and all tests are green.
Today I have tried to run same tests against chrome with this configuration:
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome)
end
Capybara.javascript_driver = :selenium
Capybara.default_driver= :selenium
Capybara.app_host = ENV['WEB_SERVER_URL']
When I start the tests they fail becouse Chrome it seems too quick, infact with Firefox to complete the form it took several seconds, instead with Chrome is very very quick but then the tests fails with random errors:
"email" field not found (??)
current_url is not the expected url (?? in the browser I see the correct url!! )
ect ect..
Errors in my opinion have no sense and is very strange because with Firefox all tests are green.
Have you ever noticed this problem?
Chromedriver with Chrome 44 returns from actions much faster than before (incorrectly apparently) so visit is basically fully asynchronous. There have been numerous issues filed against Chromedriver for this such as https://code.google.com/p/chromedriver/issues/detail?id=1158&sort=-id&colspec=ID%20Status%20Pri%20Owner%20Summary
What this means for test stability is potentially needing to specify a longer wait on finds for the first element you're looking for after a visit, and checking for content that should be on the page before checking the current url (because content checks will use capybaras waiting behavior while capybara doesn't provide a waiting url matcher currently). You could also revert to Chrome 43 which will probably fix your issues
In accordance with the link provided by Tom Walpole I have switch from Chrome 44 to Chromium 43. I prefer use Chromium becouse I use chrome during my working day and I want to always have the latest version.
In my Ubuntu 14.04:
$sudo apt-get install chromium-browser
Then:
#spec/spec_helper.rb
require 'selenium/webdriver'
if ENV["USE_CHROME_BROWSER"]
Capybara.register_driver :selenium do |app|
Selenium::WebDriver::Chrome.path = ENV["CHROME_PATH"] if ENV["CHROME_PATH"]
Capybara::Selenium::Driver.new app, browser: :chrome
end
end
Capybara.default_driver = Capybara.javascript_driver = :selenium
Capybara.app_host = ENV['WEB_SERVER_URL']
With this configuration I can easily switch from Firefox (default) to Chromium.
I use dotenv gem to manage the configurations:
#./.env
...
USE_CHROME_BROWSER = true
CHROME_PATH = "/usr/bin/chromium-browser"
...
Based on what you have provided, I cannot tell if Chrome actually does load faster than Firefox. However, the following may be of some help. Wherever you define your Capybara conditions, I suggest replacing them with the following:
Capybara.configure do |config|
config.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, browser: :chrome)
config.default_wait_time = 5 # default is 2 seconds
end
config.javascript_driver = :selenium
config.default_driver= :selenium
config.app_host = ENV['WEB_SERVER_URL']
end
Not only can you change the default wait time for Capybara to search for an element, but your code will be a little DRYer!
You may want to test other JavaScript drivers, like Capybara-Webkit. If this works for Chrome, I would suggest editing the config above, so capybara-webkit is set only when using a Chrome browser.

Resources