How do I correct this Selenium initialisation command deprecation warning?
Asked Answered
B

5

21

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)

Briton answered 5/11, 2021 at 9:31 Comment(0)
C
22

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
Contain answered 11/12, 2021 at 9:56 Comment(2)
In addition to using add_argument to add a command line argument, you may also need to use add_preference to add key/value settings.Gerrigerrie
If it's this complicated, I think I don't want to know.Peggie
E
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
Edict answered 5/11, 2021 at 21:12 Comment(0)
O
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
Okhotsk answered 1/2, 2022 at 22:57 Comment(0)
P
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.

Potluck answered 3/2, 2022 at 16:54 Comment(1)
I have 3.38.0 and still see the warning.Marvel
S
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
Smelser answered 5/3, 2022 at 0:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.