Using ChromeDriver to use Headless Chrome with Capybara.
# install the latest version
brew install chromedriver
# update existing chrome driver
brew upgrade chromedriver
chromedriver -v
# It should display the latest/specify version like so
#=> ChromeDriver 2.33.506106 (8a06c39c4582fbfbab6966dbb1c38a9173bfb1a2)
# frozen_string_literal: true
require 'capybara/rspec'
IS_DEBUG_MODE = -> { ENV['DEBUG'].present? ? :chrome : :headless_chrome }
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app, browser: :chrome)
end
Capybara.register_driver :headless_chrome do |app|
options = ::Selenium::WebDriver::Chrome::Options.new
options.add_argument 'headless'
Capybara::Selenium::Driver.new app, browser: :chrome, options: options
end
Capybara.configure do |config|
config.default_max_wait_time = 30
config.default_driver = IS_DEBUG_MODE.call
config.javascript_driver = IS_DEBUG_MODE.call
end
Update 4/10/17
RSpec has just released 3.7 version last month and the configuration for integration test is a lot cleaner. Here’s the snippet:
# System tests use Rack::Test for non JS test and headless Chrome for JS specs
config.before(:each, type: :system) do
driven_by :rack_test
end
config.before(:each, type: :system, js: true) do
driven_by :selenium_chrome_headless
end
dependencies:
pre:
# install chrome
- wget -N https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb -P ~/
- sudo dpkg -i --force-depends ~/google-chrome-stable_current_amd64.deb
- sudo apt-get -f install -y
- sudo dpkg -i --force-depends ~/google-chrome-stable_current_amd64.deb
- sudo rm /usr/local/bin/chromedriver
# install chromedriver
- wget -N http://chromedriver.storage.googleapis.com/$CHROME_DRIVER_VERSION/chromedriver_linux64.zip -P ~/
- unzip ~/chromedriver_linux64.zip -d ~/
- rm ~/chromedriver_linux64.zip
- sudo mv -f ~/chromedriver /usr/local/bin/chromedriver
- sudo chown root:root /usr/local/bin/chromedriver
- sudo chmod 0755 /usr/local/bin/chromedriver
From us to your inbox weekly.