20

Using Rails 6 I'm trying to set up selenium in headless mode for system tests, I'm using this statement in application_system_test_case.db:

driven_by :selenium, using: :headless_chrome, screen_size: [1400, 1400]

(according to Agile Web Dev Rails 6 tutorial)

but it gives me this deprecation warning:

Selenium [DEPRECATION] [:browser_options] :options as a parameter for driver initialization is deprecated. Use :capabilities with an Array of value capabilities/options if necessary instead.

I've done some searching in Selenium docs but I my basic code skills still leave me unclear as to how I should correct this. Can anyone advise how I can correct this?

(My amateur guesswork trials of things like:

driven_by :selenium, :capabilities['headless_chrome', 'screen_size: 1400, 1400']

all result in errors)

jbk
  • 1,911
  • 19
  • 36

5 Answers5

21

Updated version for the new warning with options instead of capabilities

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.register_driver :chrome do |app|
  options = Selenium::WebDriver::Chrome::Options.new

  Capybara::Selenium::Driver.new(
    app,
    browser: :chrome,
    options: options
  )
end

Capybara.default_driver = :chrome
Dorian
  • 7,749
  • 4
  • 38
  • 57
12

In Selenium 4, the options must be passed in array capabilities:

def selenium_options
  options = Selenium::WebDriver::Chrome::Options.new
  options.add_argument('--headless')
  options
end

# optional
def selenium_capabilities_chrome
  Selenium::WebDriver::Remote::Capabilities.chrome
end

def driver_init
  caps = [
    selenium_options,
    selenium_capabilities_chrome,
  ]

  Selenium::WebDriver.for(:chrome, capabilities: caps)
end

driver = driver_init
Armando
  • 603
  • 1
  • 5
  • 14
8

I stumbled upon this thread a couple of times already. What was bothering to me were not only the deprecated messages, but also the puma server logs while launching the test suite. I've ended up fixing the deprecation warning and shutting the puma logs. Here's my current setup:

class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
  # provides devise methods such as login_session
  include Devise::Test::IntegrationHelpers

  # removes noisy logs when launching tests
  Capybara.server = :puma, { Silent: true }

  Capybara.register_driver :headless_chrome do |app|
    options = Selenium::WebDriver::Chrome::Options.new(args: %w[headless window-size=1400,1000])
    Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
  end

  Capybara.register_driver(:chrome) do |app|
    options = Selenium::WebDriver::Chrome::Options.new(args: %w[window-size=1400,1000])
    Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
  end

  ENV['HEADLESS'] ? driven_by(:headless_chrome) : driven_by(:chrome)
end

So you can launch tests, for instance, with:

HEADLESS=1 rails test:all
Flavio Wuensche
  • 9,460
  • 1
  • 57
  • 54
3

Suppress the warning

One line fix:

# rails_helper.rb
Selenium::WebDriver.logger.ignore(:browser_options)

Suggested here

OR

(probably) Any version of Capybara > 3.36.0

Edit: @silvia96 has 3.38.0 and still getting warning


It's quite a confusing bug because if you look at the Capybara driver registration you can see it already knows about using capabilities. The actual bug is because the Gem version test is set as ~ instead of >=. The fix is in main and any version of Capybara after 3.36.0 will, likely, fix it.

notapatch
  • 6,569
  • 6
  • 41
  • 45
0

Combining the useful solutions from others, here's what mine looks like. Remove puma logs, make headless chrome, ignore browser options error:

require "test_helper"

class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
  driven_by :selenium, using: :headless_chrome
  Capybara.server = :puma, { Silent: true }
  Selenium::WebDriver.logger.ignore(:browser_options)

end
Cody
  • 436
  • 5
  • 13