Skip to content

Instantly share code, notes, and snippets.

@vhorb
Last active December 18, 2023 11:36
Show Gist options
  • Save vhorb/dec95ecaf6f4899236a610f0d0a0dba9 to your computer and use it in GitHub Desktop.
Save vhorb/dec95ecaf6f4899236a610f0d0a0dba9 to your computer and use it in GitHub Desktop.
Capybara register new driver for test
# Typically this faile named capybara.rb and needed for browser configuration.
# This file related to capybara 3.22.0 and webdriver 3.8.0
require 'capybara/rspec'
require 'capybara-screenshot/rspec'
require 'webdrivers'
Capybara.register_driver :context_selenium_chrome do |app|
chrome_args = %w[window-size=1920,1080]
# for headless can be used config file or ENV variable.
chrome_args += %w[headless disable-gpu] if APP_CONFIG.dig('headless')
# after gem updates chromeOptions changed to 'goog:chromeOptions' - https://github.com/elgalu/docker-selenium/issues/201
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
'goog:chromeOptions': { args: chrome_args }
)
Capybara::Selenium::Driver.new app,
browser: :chrome,
desired_capabilities: capabilities
end
Capybara::Screenshot.register_driver(:context_selenium_chrome) do |driver, path|
driver.browser.save_screenshot(path)
end
# Available drivers: :rack_test, :selenium, :selenium_chrome, :selenium_chrome_headless, :chrome
Capybara.default_driver = :context_selenium_chrome
Capybara.javascript_driver = :context_selenium_chrome
# option to hide puma start logs
Capybara.server = :puma, { Silent: true }
@baweaver
Copy link

If you happened to encounter SeleniumHQ/selenium#2228 this will also auto open devtools for you in Chrome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment