How to use headless Chrome with Capybara and Selenium
Asked Answered
C

6

16

Chrome version: 59.0.3071.104

I am using Cucumber, Capybara, Selenium to implement automation testing with Headless Chrome.

features/support/env.rb

require 'rubygems'
require 'capybara/cucumber'

Capybara.register_driver :selenium_chrome do |app|
  Capybara::Selenium::Driver.new(app, :browser => :chrome, args: ['headless'])
end

Capybara.default_driver = :selenium_chrome

When running a cucumber test, it says:

WARN Selenium [DEPRECATION] :args or :switches is deprecated. Use Selenium::WebDriver::Chrome::Options#add_argument instead.

What is the correct way to use capybara and selenium with Headless Chrome. Thank you.

I tried the following using Ruby-Binding, Capybara and Working Example. But it gives errors as well.

require 'rubygems'
require 'capybara/cucumber'
require 'selenium-webdriver'

Capybara.register_driver :selenium_chrome do |app|
  options = Selenium::WebDriver::Chrome::Options.new
  options.add_argument('--headless')
  driver = Selenium::WebDriver.for :chrome, options: options
  Capybara::Selenium::Driver.new(
    app,
    browser: :chrome,
    desired_capabilities: driver
  )
end

Capybara.default_driver = :selenium_chrome

Error:

> undefined method `each' for
> #<Selenium::WebDriver::Remote::Capabilities:0xd13baac> (NoMethodError)

I downloaded the latest ChromeDriver 2.30.

Using the example from ChromeDriver Capabilities and Chrome Command Line Switches, I get an error.

Net::ReadTimeout (Net::ReadTimeout)

require 'rubygems'
require 'capybara/cucumber'
require 'selenium-webdriver'

Capybara.register_driver :selenium_chrome do |app|
  caps = Selenium::WebDriver::Remote::Capabilities.chrome(
    "chromeOptions" => {
      "binary" => "/chromedriver_win32/chromedriver.exe",
      "args" => [ "--disable-web-security", "--headless" ]
    }
  )
  Capybara::Selenium::Driver.new(
    app,
    browser: :chrome,
    desired_capabilities: caps
  )
end

Capybara.default_driver = :selenium_chrome
Casals answered 16/6, 2017 at 14:19 Comment(0)
P
13

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
Pagano answered 22/6, 2017 at 10:8 Comment(1)
I used the same but getting Selenium::WebDriver::Error::InvalidArgumentError: invalid argument (Session info: headless chrome=93.0.4577.63)Minelayer
L
10

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 isn't any 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 the test running on it:

CHROME_HEADLESS=false bin/rails test:system
Leadsman answered 29/5, 2018 at 16:1 Comment(0)
C
2

Windows support is coming in Chrome 60.

Download "Chome Canary" and give the installation path as a binary in chromeOptions.

Download the latest "chromedriver" and add to path in Environment Variables.

require 'rubygems'
require 'capybara/cucumber'
require 'selenium-webdriver'

Capybara.register_driver :selenium_chrome do |app|
  caps = Selenium::WebDriver::Remote::Capabilities.chrome(
    "chromeOptions" => {
      "binary" => "C:/Users/YOURUSERNAME/AppData/Local/Google/Chrome SxS/Application/chrome.exe",
      "args" => [ "--disable-web-security", "--headless", "--disable-gpu" ]
    }
  )
  Capybara::Selenium::Driver.new(
    app, 
    browser: :chrome, 
    desired_capabilities: caps
  )
end

Capybara.default_driver = :selenium_chrome
Casals answered 16/6, 2017 at 18:49 Comment(0)
B
0

I could get this working with the current version of Chrome, but it would display a blank window. If you want to get rid of that, then you need to use the "Chrome Canary" build.

The code below is a combination of the answers that @lucas-caton and @shawn-derik, but solves the issue I mentioned above.

Capybara.register_driver :chrome do |app|
  Capybara::Selenium::Driver.new(
    app,
    browser: :chrome,
    options: Selenium::WebDriver::Chrome::Options.new(
      binary: 'C:/Users/USERNAME/AppData/Local/Google/Chrome SxS/Application/chrome.exe',
      args: %w[no-sandbox headless disable-gpu]
    )
  )
end
Bud answered 17/7, 2017 at 13:42 Comment(0)
A
0

March 2024 update:

  1. Run sudo apt install chromium-chromedriver in the underlying OS.
  2. Put the following contents into corresponding files:

test helper:

Capybara.current_driver = :selenium_chrome_headless

Gemfile

gem 'selenium-webdriver'
Argenteuil answered 2/3, 2024 at 9:32 Comment(1)
Why only one single quote?Pimiento
O
0

I had a surprisingly hard time getting my rspec + capybara + chrome (headful) system specs to run with chrome headless.

What worked for me

Add this to the very end of spec/rails_helper.rb (not inside any other block/code, just at the very end of the file on its own).

# spec/rails_helper.rb

if ENV['HEADLESS'] == "true"
  RSpec.configure do |config|
    config.before(:each, type: :system) do
      driven_by :selenium_chrome_headless
    end
  end
end

Now you can do

# run specs in chrome headless
HEADLESS=true bundle exec rspec

# run specs in chrome normally (headful)
bundle exec rspec

Note

Note that if you don't care about having the option and just want headless all the time, just plonk this at the end of spec/rails_helper.rb

RSpec.configure do |config|
  config.before(:each, type: :system) do
    driven_by :selenium_chrome_headless
  end
end
Only answered 31/3, 2024 at 6:40 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.