I've currently got a Rails application trying to get headless chrome working for testing.
Capybara Version: 2.15.1
Selenium Webdriver: 3.11.0
Using docker image below: https://github.com/SeleniumHQ/docker-selenium**
Without the headless option, I see the browser boots up and the tests run without a problem. However, when I add the headless argument to the capabilities, the tests consistently fail with unable to find element. When I look at the screenshots/html, all I see are blank webpages like such and the PNG screenshot is a completely blank white screen.
<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body></body></html>
Here is the selenium helper file I'm using to setup the drivers.
Capybara.register_driver :remote_browser do |app|
  selenium = ENV.fetch('SELENIUM_ADDR', 'selenium')
  WebMock.disable_net_connect!(
    :allow => [
      'api.knapsackpro.com',
      selenium,
      Capybara.server_host,
    ],
  )
  capabilities = {
    "browserName" => "chrome",
    "goog:chromeOptions" => {
      args: %w[headless disable-gpu window-size=1920,1080 no-sandbox]
    }
  }
  Capybara::Selenium::Driver.new(app, browser: :remote, url: "http://#{selenium}:4444/wd/hub", desired_capabilities: capabilities)
end
Capybara.javascript_driver = :remote_browser
Capybara::Screenshot.register_driver(:remote_browser) do |driver, path|
  driver.browser.save_screenshot(File.join('..', path))
end
You can try this approach:
in Gemfile:
group :test do
  gem 'capybara', '~> 3.31'
  gem 'capybara-screenshot', '~> 1.0'
  gem 'selenium-webdriver', '~> 3.142'
in spec/rails_helper.rb:
Capybara.register_driver :headless_chrome do |app|
  options = ::Selenium::WebDriver::Chrome::Options.new
  options.add_argument('--headless')
  options.add_argument('--no-sandbox')
  options.add_argument('--disable-dev-shm-usage')
  options.add_argument('--disable-extensions')
  options.add_argument('--disable-gpu')
  options.add_argument('--window-size=1920,1080')
  options.add_argument('disable-infobars')
  Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
end
Capybara.javascript_driver = :headless_chrome
Capybara::Screenshot.register_driver(:headless_chrome) do |driver, path|
  driver.browser.save_screenshot(path)
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With