Skip to content

Instantly share code, notes, and snippets.

@matthewrudy
Created June 26, 2017 09:58
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save matthewrudy/d6c4c3709ff3d61ee24d7eb7aa117c9f to your computer and use it in GitHub Desktop.
Save matthewrudy/d6c4c3709ff3d61ee24d7eb7aa117c9f to your computer and use it in GitHub Desktop.
Running headless chrome on capybara with selenium webdriver
require "selenium/webdriver"
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app, browser: :chrome)
end
Capybara.register_driver :headless_chrome do |app|
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
chromeOptions: { args: %w(headless disable-gpu) }
)
Capybara::Selenium::Driver.new app,
browser: :chrome,
desired_capabilities: capabilities
end
Capybara.default_driver = ENV['CODESHIP'] ? :chrome : :headless_chrome
@lcguida
Copy link

lcguida commented Jan 22, 2022

2022 Update:

Replace chromeOptions with goog:chromeOptions

"goog:chromeOptions": { args: %w(headless disable-gpu) }

@alazycoder101
Copy link

For some reason headless is not working for me. I have to use the following one:

Capybara.register_driver :headless_chrome do |app|
   options = Selenium::WebDriver::Chrome::Options.new(
     args: %w[headless no-sandbox disable-gpu disable-dev-shm-usage],
     binary: "/usr/bin/google-chrome"
   )

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

@tomfast
Copy link

tomfast commented Feb 2, 2023

Thanks for this @lcguida and @alazycoder101 . 👊🚀

@sytzez
Copy link

sytzez commented Mar 17, 2023

This worked for me, with no deprecation errors:

Capybara.register_driver :headless_chrome do |app|
  options = Selenium::WebDriver::Chrome::Options.new(
    args: %w[headless no-sandbox disable-gpu disable-dev-shm-usage],
  )

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

@rtsinani
Copy link

Thanks @sytzez 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment