Created
April 7, 2025 18:43
-
-
Save armandofox/6e81a496d8f094b59f4da87f13034fa0 to your computer and use it in GitHub Desktop.
env.rb stuff to include selenium/webdriver using headless ChromeForTesting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# must have compatible versions of chromedriver and chrome-for-testing (headless) installed: | |
# Download a specific Chrome for Testing version: | |
# npx @puppeteer/browsers install [email protected] | |
# Download a specific ChromeDriver version: | |
# npx @puppeteer/browsers install [email protected] | |
# (Note: these 'installs' just put stuff in the $cwd. brew install may be better) | |
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 | |
Capybara.javascript_driver = :selenium_chrome_headless |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment