How to update Capybara profile? - ruby

I am using code like this to set up Capybara profile:
Capybara.register_driver :selenium_focus do |app|
profile = Selenium::WebDriver::Firefox::Profile.new
Capybara::Selenium::Driver.new(app, browser: :firefox, profile: profile)
end
Later in the setup process, I want to update the profile. For example:
profile["focusmanager.testmode"] = true
How to update the profile after it has been set up with profile["focusmanager.testmode"] = true?

It's not really possible. The easiest solution is to register 2 drivers with the different settings wanted and then use the correct driver for each test

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...

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 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 might I simulate a private browsing experience in Watir? (Selenium)

Watir is a Selenium-based black-box testing tool that can automate tasks on the browser.
I would like to be able to open up a Watir::Browser.new that is in private browsing mode.
Thanks
For Firefox (I am not sure about the other browsers), you can setup the profile to have private browsing enabled:
profile = Selenium::WebDriver::Firefox::Profile.new
profile['browser.privatebrowsing.dont_prompt_on_enter'] = true
profile['browser.privatebrowsing.autostart'] = true
b = Watir::Browser.new :firefox, :profile => profile
This was the solution from https://github.com/watir/watir-webdriver/issues/95.
It seems to work (at least the main menu says it is private browsing).
For Chrome
browser = Watir::Browser.new :chrome, switches: ['--incognito']

Resources