how do I set elementScrollBehavior to 1 using the ruby bindings for Selenium:WebDriver - ruby

In the context of a capybara test, I'm trying to set elementScrollBehavior to 1 (instructing webdriver to scroll elements targeted for interaction to the bottom of the viewport). I sort of expected this to work:
Capybara.register_driver :selenium do |app|
capabilities = Selenium::WebDriver::Remote::Capabilities.firefox('elementScrollBehavior' => 1)
Capybara::Selenium::Driver.new(app, desired_capabilities: capabilities)
end
but it does not.

Related

Firefox doesn't respect my desired_capability to not load images

I used profile["permissions.default.image"] = 2 earlier but now it doesn't work for me:
require "capybara/rspec"
require "selenium/webdriver"
Capybara.register_driver :my_driver do |app|
profile = Selenium::WebDriver::Firefox::Profile.new
profile["permissions.default.image"] = 2
desired_capabilities = Selenium::WebDriver::Remote::Capabilities.firefox firefor_profile: profile
Capybara::Selenium::Driver.new(app,
desired_capabilities: desired_capabilities,
browser: :firefox,
)
end
Capybara.default_driver = :my_driver
feature do
scenario do
visit "http://lenta.ru"
STDIN.gets
end
end
I still see images.
$ /Applications/Firefox.app/Contents/MacOS/firefox-bin -v
Mozilla Firefox 38.3.0
gem "selenium-webdriver", "2.48.0"

Changing default drivers using Capybara per scenario

I have written a small block that will launch Google Chrome when I set a tag #chrome above a scenario within a feature file.
Before ('#chrome') do
Capybara.default_driver = :selenium
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app,
:browser => :chrome,
desired_capabilities: {
'chromeOptions' => {
'args' => %w{ window-size=1920,1080 }
}
}
)
end
end
The problem I have is that all subsequent scenarios within the feature file will also run in Chrome, even when the tag isn't set for them.
Is there a way to add to it to say revert back to Poltergeist once I'm done. I've tried the following but it didn't work:
After do |scenario|
if #chrome == true
Capybara.register_driver = :poltergeist
Capybara::Poltergeist::Driver.new(
app,
phantomjs_options: ['--ignore-ssl-errors=yes', '--ssl-protocol=TLSv1'],
window_size: [1280, 1024],
js_errors: false,
debug: false
)
end
end
Thanks in advance for any help you might have
Capybara already provides this behavior here . To use it you just need to require it and register a driver with the tag name you want to use. This would usually be in your env.rb/custom_env.rb
require 'capybara/cucumber'
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app,
browser: :chrome,
...
)
end
The important thing is the name you use to register the driver has to match the tag used (in this case #chrome). It also shows you shouldn't be changing Capybara.default_driver on a test by test basis, that's what Capybara.current_driver is for. You also shouldn't be registering a new driver every scenario, register_driver is meant to be called once for each driver configuration you will need and then referenced by name later.

firefox profile setting not updating correctly through selenium

I am trying to set the firefox profile so that all links will open in the same tab when running my selenium tests.
I have found the setting required to do this, howvever when the program runs it is not being set to the value i want, whereas other values are.
Heres my code
profile = Selenium::WebDriver::Firefox::Profile.new
profile['browser.link.open_newwindow.restriction'] = 0
profile['browser.link.open_newwindow'] = 1
#browser = Selenium::WebDriver.for :firefox, :profile => profile
the loaded broswer page has the 'browser.link.open_newwindow' set to 2, which is not the default setting and the browser indicates that the value 2 has been user set, even though it is not what I have set it to be
does anyone know why this maybe happening? does selenium or the page-object-gem write this value?
If you are using Capybara, have this in your features/support.env.rb file:
Capybara.register_driver :selenium do |app|
profile = Selenium::WebDriver::Firefox::Profile.new
profile['browser.link.open_newwindow.restriction'] = 0
profile['browser.link.open_newwindow'] = 1
Capybara::Selenium::Driver.new(app, :browser => :firefox, :profile => profile)
end

How to stabilize IE windows in capybara/ruby environment

Tests that I run well in Chrome and Firefox appear flaky or blinky in IE9. I understand a java based fix for this is setting a capability
caps.setCapability("requireWindowFocus", true);
I interpreted a rough ruby attempt in the env.rb...
Capybara.default_driver = :selenium
Capybara.register_driver :selenium do |app|
case get_browser
when 'ie'
caps = Selenium::WebDriver::Remote::Capabilities.internet_explorer
caps.setCapability("requireWindowFocus", true)
Capybara::Selenium::Driver.new(app, :browser => :internet_explorer, :desired_capabilities => caps)
else
Capybara::Selenium::Driver.new(app, :browser => :chrome)
end
end
and caught the following nomethod error
NoMethodError: undefined method `setCapability' for #<Selenium::WebDriver::Remote::Capabilities:0x3e9b218>
A ruby fix for this will be greatly appreciated.
Based on the Ruby bindings page, the capabilities are set like:
caps = Selenium::WebDriver::Remote::Capabilities.internet_explorer
caps["requireWindowFocus"] = true
Capybara::Selenium::Driver.new(app, :browser => :internet_explorer, :desired_capabilities => caps)

Getting Cucumber/Capybara to close and open another browser after scenarios

Say I have 3 Scenario Outlines and I need to run scenario 1 with Firefox, close the browser, then run scenario 2 with Chrome, close the browser, then finally run scenario 3 with Firefox.
Is there any way to close/quit the browser in Cucumber/Capybara after each scenario?
So far I have registered two drivers, one for Firefox and one for Chrome:
Capybara.register_driver :selenium_firefox do |app|
Capybara::Driver::Selenium.new(app, :browser => :firefox)
end
Capybara.register_driver :selenium_chrome do |app|
Capybara::Driver::Selenium.new(app, :browser => :chrome)
end
Then in hooks.rb, I'm using a 'custom' tag "#alternate_browser"
Before('#alternate_browser') do
driver = Capybara.current_driver
if driver == :selenium_firefox
Capybara.current_driver = :selenium_chrome
else
Capybara.current_driver = :selenium_firefox
end
end
Is there a way to force the browser to close after each scenario?
EDIT: I tried:
page.evaluate_script("window.close()")
page.execute_script("window.close()")
But both statements had no effect.
This is what you should do.
Stop closing the windows.
Change to explicit browser tags (#firefox,#chrome)
I haven't tried it but this should work.
Capybara.register_driver :selenium_firefox do |app|
Capybara::Driver::Selenium.new(app, :browser => :firefox)
end
Capybara.register_driver :selenium_chrome do |app|
Capybara::Driver::Selenium.new(app, :browser => :chrome)
end
Before('#firefox') do
Capybara.current_driver = :selenium_firefox
end
Before('#chrome') do
Capybara.current_driver = :selenium_chrome
end

Resources