Disable Javascript for Chrome using Capybara Selenium - ruby

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

Related

Overwrite Chrome option mid test

I would like to test a single scenario that something appears when JS is disabled. I only want JS removed for this one scenario.
Can I temporarily overwrite a chrome setting?
My env.rb looks like this:
Capybara.register_driver :chrome do |app|
opts = Selenium::WebDriver::Chrome::Options.new
opts.add_argument '--start-maximized'
opts.add_argument '--disable-infobars'
opts.add_argument '--headless' if Settings.browser.headless
opts.add_argument('--enable-javascript')
unless Settings.browser.sandbox
opts.add_argument '--no-sandbox'
opts.add_argument '--disable-dev-shm-usage'
end
caps = Selenium::WebDriver::Remote::Capabilities.new(accept_insecure_certs: true)
caps.browser_name = 'chrome'
Capybara::Selenium::Driver.new(app,
browser: :chrome,
options: opts,
desired_capabilities: caps)
end
I would like to overwrite the setting to disable Javascript.
Is this possible?
You can't modify the current drivers settings mid run, but you can register another driver and use it for just the one scenario.
Capybara.register_driver :chrome_no_js do |app|
... # setup driver with the desired options, etc
end
Then in your features, assuming you're using the normal Capybara/Cucumber config, you can tag your scenario to use the new driver
#chrome_no_js
Scenario: do something with js disabled
...
Note: there's no guarantee everything will work correctly with JS disabled since Capybara has to use JS to work around bugs/limitations...

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?

How to configure Selenium web driver to start a chrome browser in the background

I am using Selenium and trying to configure the Chrome driver to start in the background, that is I don't want the window to take focus. The idea is that running the test suite does not disrupt my flow when I am coding. I want the window focus to remain on the actual code editor, not on the newly created chrome window.
So I found this answer: Selenium - chrome Driver fail to start in background (without a start-up window)
But it uses Java. I need a Ruby solution.
I am currently using the Chrome driver as this:
Capybara.default_driver = :chrome
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome)
end
How can I achieve what I described?
Translating from the original Java, you just need to add the startup argument to your Capabilities and pass that to the new Driver instance:
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
"chromeOptions" => {"args" => [ "--no-startup-window" ]})
Capybara.default_driver = :chrome
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome,
:desired_capabilities => capabilities)
end
Reference: https://sites.google.com/a/chromium.org/chromedriver/capabilities

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.

How to use capybara with watir / watir-webdriver

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.

Resources