Skip to content

Instantly share code, notes, and snippets.

@nruth
Last active March 22, 2023 13:10
Show Gist options
  • Star 23 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nruth/864dc9875b4feb183b7b10ddbd25c7f4 to your computer and use it in GitHub Desktop.
Save nruth/864dc9875b4feb183b7b10ddbd25c7f4 to your computer and use it in GitHub Desktop.
translating old capybara selenium/chrome preferences and switches to new
# load into test setup after `require 'capybara/rails'`
# some sources for below flags and profile settings
# https://stackoverflow.com/questions/43143014/chrome-is-being-controlled-by-automated-test-software/43145088
# https://sqa.stackexchange.com/questions/26051/chrome-driver-2-28-chrome-is-being-controlled-by-automated-test-software-notif
# http://stackoverflow.com/questions/12211781/how-to-maximize-window-in-chrome-using-webdriver-python
# update sources for new Options object
# https://github.com/SeleniumHQ/selenium/wiki/Ruby-Bindings
# https://github.com/teamcapybara/capybara/blob/master/lib/capybara/selenium/driver.rb
begin
chrome_switches = %w[
--disable-default-apps
--disable-extensions
--disable-infobars
--disable-notifications
--disable-password-generation --disable-password-manager-reauthentication
--disable-password-separated-signin-flow
--disable-popup-blocking
--disable-save-password-bubble
--disable-translate
--incognito
--mute-audio
--no-default-browser-check
--window-size=1280,1024
]
# --ignore-certificate-errors
chrome_options = Selenium::WebDriver::Chrome::Options.new
chrome_switches.each do |option|
chrome_options.add_argument(option)
end
chrome_options.add_preference(:download, { prompt_for_download: false })
chrome_options.add_preference(:credentials_enable_service, false)
chrome_options.add_preference(:profile,
{
password_manager_enabled: false,
default_content_settings: {popups: 0},
content_settings: {pattern_pairs: {'*': {'multiple-automatic-downloads': 1}}}
}
)
Capybara.register_driver :selenium do |app|
# according to https://github.com/SeleniumHQ/selenium/blob/master/rb/lib/selenium/webdriver/remote/http/default.rb
# Warning: Setting {#open_timeout} to non-nil values will cause a separate thread to spawn.
# Debuggers that freeze the process will not be able to evaluate any operations if that happens.
client = Selenium::WebDriver::Remote::Http::Default.new(open_timeout: nil, read_timeout: 120)
Capybara::Selenium::Driver.new(
app,
browser: :chrome, clear_local_storage: true, clear_session_storage: true,
options: chrome_options,
http_client: client
)
end
Capybara.register_driver :selenium_chrome_headless do |app|
chrome_options.add_argument("--headless")
# according to https://github.com/SeleniumHQ/selenium/blob/master/rb/lib/selenium/webdriver/remote/http/default.rb
# Warning: Setting {#open_timeout} to non-nil values will cause a separate thread to spawn.
# Debuggers that freeze the process will not be able to evaluate any operations if that happens.
client = Selenium::WebDriver::Remote::Http::Default.new(open_timeout: nil, read_timeout: 120)
Capybara::Selenium::Driver.new(
app,
browser: :chrome, clear_local_storage: true, clear_session_storage: true,
options: chrome_options,
http_client: client
)
end
end
Capybara.register_driver :selenium_firefox do |app|
Capybara::Selenium::Driver.new(app,
browser: :firefox, clear_local_storage: true, clear_session_storage: true
)
end
Capybara.register_driver :selenium_cookies_disabled do |app|
profile = Selenium::WebDriver::Firefox::Profile.new
profile['network.cookie.cookieBehavior'] = 2
Capybara::Selenium::Driver.new(app,
clear_local_storage: true, clear_session_storage: true,
browser: :firefox, profile: profile
)
end
# maximise the browser window to avoid clicking errors at small window sizes
RSpec.configure do |config|
config.before(:each, js: true) do
if (
[:selenium_firefox, :selenium_cookies_disabled]
).include?(Capybara.current_driver)
then
Capybara.page.driver.browser.manage.window.maximize
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment