Skip to content

Instantly share code, notes, and snippets.

@armandofox
Created April 17, 2025 21:04
Show Gist options
  • Save armandofox/29d09a904c08c26f5e50cea60219f2fc to your computer and use it in GitHub Desktop.
Save armandofox/29d09a904c08c26f5e50cea60219f2fc to your computer and use it in GitHub Desktop.
Running headless ChromeForTesting for your Cuke/Capybara tests
# This setup in features/support/env.rb works both locally in and CI for me.
# IN your Gemfile's `test` environment, add
# gem 'simplecov-json', require: false
# and run `bundle install` to get it.
# these steps must be at very top of features/support/env.rb *and* spec/rails_helper.rb:
require 'simplecov'
SimpleCov.start 'rails'
if ENV['CCTR'] # are we running in CI or locally? (see gist [https://gist.github.com/armandofox/6273653fa68744d720842e6a6e5be04f] )
require "simplecov_json_formatter"
SimpleCov.formatter = SimpleCov::Formatter::JSONFormatter
else
# running locally; make nice HTML output to browse coverage interactively
SimpleCov.formatter = SimpleCov::Formatter::HTMLFormatter
end
# In CI, I set the envariables CHROMEDRIVER_PATH and CHROME_FOR_TESTING_PATH based on using
# an existing Github Action that installs those binaries.
# When running locally, these variables are NOT set, but I have both ChromeForTesting and Chromedriver
# (matching versions) installed in the tmp/ directory in the root of the app.
# Note carefully where `path_to_chromedriver` and `path_to_chrome_for_testing` are
# subsequently used in lines 15 and later.
path_to_chromedriver = ENV['CHROMEDRIVER_PATH'] ||
`find ~+/tmp -type f -name 'chromedriver'`.chomp
path_to_chrome_for_testing = ENV['CHROME_FOR_TESTING_PATH'] ||
`find ~+/tmp -type f -name 'Google Chrome for Testing'`.chomp
if (path_to_chromedriver.blank? || path_to_chrome_for_testing.blank?)
abort "Cannot find Chromedriver and/or ChromeForTesting binaries. Check wiki for instructions."
end
Capybara.register_driver :selenium_chrome_headless do |app|
options = Selenium::WebDriver::Chrome::Options.new.tap do |opts|
opts.add_argument '--headless'
opts.add_argument '--no-sandbox'
opts.add_argument '--disable-gpu'
opts.add_argument '--window-size=1024,1024'
# When an "unexpected" alert/confirm is displayed, accept it (ie user clicks OK).
# Expected ones can be handled with accept_alert do...end or accept_confirm do...end
opts.unhandled_prompt_behavior = :accept
opts.binary = path_to_chrome_for_testing
end
# expects headless Chrome-for-testing and its driver to be in $RAILS_ROOT/tmp somewhere,
# but puppeteer installs them in arch-specific subdirs :-(
Capybara::Selenium::Driver.new(
app,
browser: :chrome,
service: Selenium::WebDriver::Service.chrome(path: path_to_chromedriver),
options: options,
clear_session_storage: true,
clear_local_storage: true)
end
# note that I use headless *only* for scenarios marked @javascript. Otherwise it uses the much faster Rack::Test.
# To accomplish that, set `Capybara.javascript_driver` rather than just `Capybara.driver`.
Capybara.javascript_driver = :selenium_chrome_headless
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment