10

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
Kai Mou
  • 284
  • 3
  • 20
  • are you sure about this? `window-size=19280,1080` you probably mean `window-size=1920,1080` – lacostenycoder Mar 22 '18 at 14:57
  • @lacostenycoder Yeah sorry. I accidentally added an extra 0 when I copy pasted the code over. – Kai Mou Mar 22 '18 at 15:02
  • Did you ever solve this? I have similar problem, where the screenshots are blank. I suspect that the driver is closing before the screenshot or something, but lots of Googling and really no closer than I was. – codenoob Nov 28 '19 at 15:21
  • 1
    I too am having this issue - was it ever solved? – user3437721 Dec 24 '19 at 10:30
  • I get a similar issue when running tests remotely from a Jenkins server: https://stackoverflow.com/questions/58629302/selenium-doesnt-refresh-page-on-jenkins. – Mate Mrše Feb 28 '20 at 18:53

1 Answers1

1

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
Sergio Belevskij
  • 2,478
  • 25
  • 24