automation-novice
automation-novice

Reputation: 31

Browser is launching for headless chrome capybara with Selenium webdriver

I'm trying to launch chrome in headless but the browser keeps on launching. Tried several different ways

  1. used chrome options and added arguments
  2. used chrome capabilities as well

My chrome version: 86 OS - ubuntu capybara - 3.32.2

First Helper file:

spec_helper.rb

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

Capybara.default_driver = :headless_chrome
Capybara.javascript_driver = :headless_chrome


Second try of Helper file:


Capybara.register_driver :headless_chrome do |app|
  Capybara::Selenium::Driver.new(
    app, 
    browser: :chrome,
    desired_capabilities:Selenium::WebDriver::Remote::Capabilities.chrome(
       chromeOptions: {
                       args: %w[headless disable-gpu disable-popup-blocking no-sandbox]
                      }
           )
  )
end

Capybara.default_driver = :headless_chrome
Capybara.javascript_driver = :headless_chrome

Thanks in advance

Upvotes: 1

Views: 1226

Answers (3)

Johannes
Johannes

Reputation: 514

Apparently, for Selenium >= 3.8, you have to specify goog:chromeOptions instead of chromeOptions.

So in your example:

...
Selenium::WebDriver::Remote::Capabilities.chrome(
   "goog:chromeOptions": {
                   args: %w[headless disable-gpu disable-popup-blocking no-sandbox]
                  }
       )
)

I was experiencing the same issue and this solved it for me.

Upvotes: 0

automation-novice
automation-novice

Reputation: 31

Resolved this by adding chromeOptions separately and had to remove -- from all arguments

Upvotes: 0

Thomas Walpole
Thomas Walpole

Reputation: 49870

Simplest way is to not register your own driver and just use the one provided by Capybara

Capybara.default_driver = :selenium_chrome_headless
Capybara.javascript_driver = :selenium_chrome_headless

Note - this will only work if you're not using rails system tests (no indication in your question that you're using rails though) since they override these settings - see the rails/rspec system test docs (driven_by) for that.

Upvotes: 2

Related Questions