How to use headless chrome with capybara and selenium

Update 2020-02-01

Support for ChromeDriver ended on Mar 24, 2019, creator recommended everybody to move to https://github.com/titusfortner/webdrivers, having said that, the following is a configuration that worked for me:

require "test_helper"

class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
  driver = ENV['DRIVER'] ? ENV['DRIVER'].to_sym : :headless_chrome
  driven_by :selenium, using: driver, screen_size: [1400, 1400]
end

When running your tests, you can also specify a different driver by passing the DRIVER variable like:

$ DRIVER=firefox bin/rails test:system
$ DRIVER=headless_firefox bin/rails test:system
$ DRIVER=chrome bin/rails test:system
$ DRIVER=headless_chrome bin/rails test:system

Previous Answer

The simplest way to run headless Chrome with Rails apps is to add the following gems to the Gemfile

gem 'chromedriver-helper'
gem 'selenium-webdriver'

And update your application_system_test_case.rb with the following:

require "test_helper"

class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
  driver = ENV['CHROME_HEADLESS'] == 'false' ? :selenium_chrome : :selenium_chrome_headless
  driven_by driver, screen_size: [1400, 1400]
end

There is no need to pass args as the drivers are already available to use, available drivers are :rack_test, :selenium, :selenium_chrome, :selenium_chrome_headless.

You can run Headless Chrome

$ bin/rails test:system

Or you can also run Chrome and see test running on it

$ CHROME_HEADLESS=false bin/rails test:system

1) Make sure you don't have another registered driver, I made this mistake myself and had an iphone driver, which was using the args in the old way, that's why I was getting the warning.

2) Make sure you have Chrome version 57+ on Linux, 59+ on macOS or 60+ on Windows;

3) Add/update the gem selenium-webdriver;

4) Add the following driver to your spec_helper.rb or rails_helper.rb:

Capybara.register_driver :chrome do |app|
  Capybara::Selenium::Driver.new app, browser: :chrome,
    options: Selenium::WebDriver::Chrome::Options.new(args: %w[headless disable-gpu])
end

Capybara.javascript_driver = :chrome