Skip to content

Instantly share code, notes, and snippets.

@antonfefilov
Forked from bbonamin/Brewfile
Created December 2, 2020 17:40
Show Gist options
  • Save antonfefilov/42cc62a3dae7c5744c1a72c92e431fbe to your computer and use it in GitHub Desktop.
Save antonfefilov/42cc62a3dae7c5744c1a72c92e431fbe to your computer and use it in GitHub Desktop.
Capybara Selenium Webdriver: Headless Chrome (with file downloads!) & Headless Firefox
tap "caskroom/cask"
cask "google-chrome"
cask "firefox"
brew "chromedriver"
brew "geckodriver"
require 'capybara/rspec'
require 'selenium/webdriver'
options = Selenium::WebDriver::Chrome::Options.new
options.add_preference(:download, prompt_for_download: false,
default_directory: '/tmp/downloads')
options.add_preference(:browser, set_download_behavior: { behavior: 'allow' })
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
end
Capybara.register_driver :headless_chrome do |app|
options.add_argument('--headless')
options.add_argument('--disable-gpu')
options.add_argument('--window-size=1280,800')
driver = Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
### Allow file downloads in Google Chrome when headless!!!
### https://bugs.chromium.org/p/chromium/issues/detail?id=696481#c89
bridge = driver.browser.send(:bridge)
path = '/session/:session_id/chromium/send_command'
path[':session_id'] = bridge.session_id
bridge.http.call(:post, path, cmd: 'Page.setDownloadBehavior',
params: {
behavior: 'allow',
downloadPath: '/tmp/downloads'
})
###
driver
end
Capybara.javascript_driver = ENV['GUI'] ? :chrome : :headless_chrome
require 'capybara/rspec'
require 'selenium/webdriver'
Capybara.register_driver :firefox do |app|
Capybara::Selenium::Driver.new(app, browser: :firefox)
end
Capybara.register_driver :headless_firefox do |app|
options = Selenium::WebDriver::Firefox::Options.new
options.headless! # added on https://github.com/SeleniumHQ/selenium/pull/4762
Capybara::Selenium::Driver.new app,
browser: :firefox,
options: options
end
Capybara.javascript_driver = ENV['GUI'] ? :firefox : :headless_firefox
source 'https://rubygems.org'
gem 'selenium-webdriver'
gem 'capybara'
gem 'rspec'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment