Headless Chrome with Capybara and Selenium Setup Guide
In this article
Using Headless Chrome with Capybara and Selenium allows you to run automated browser tests without a visible user interface. This significantly increases test execution speed and enables tests to run on Continuous Integration (CI) servers, such as GitHub Actions or Jenkins, where a graphical display is unavailable. By configuring the Chrome driver to run in headless mode, you maintain the full power of a real browser engine while eliminating the overhead of rendering a window.
What You'll Need
- Ruby installed on your system.
- The Google Chrome browser installed.
- The
selenium-webdrivergem. - The
capybaragem. - The
webdriversgem (or a manually installedchromedriverthat matches your Chrome version).
Step-by-Step
- Install Necessary Gems: Add the following to your
Gemfileand runbundle install. Ensure you have the latest versions to maintain compatibility with the current Chrome release.gem 'capybara' gem 'selenium-webdriver' gem 'webdrivers'
- Configure Capybara Registration: In your test helper file (e.g.,
spec_helper.rborrails_helper.rb), you must register a driver that tells Selenium to use Chrome with theheadlessargument. Use the following configuration:Capybara.register_driver :selenium_chrome_headless do |app| options = Selenium::WebDriver::Chrome::Options.new options.add_argument('--headless') options.add_argument('--disable-gpu') options.add_argument('--window-size=1280,800') Capybara::Selenium::Driver.new(app, browser: :chrome, options: options) end - Set the Default Driver: Tell Capybara to use the newly registered headless driver for your tests. You can set this globally or conditionally based on an environment variable.
Capybara.javascript_driver = :selenium_chrome_headless Capybara.default_driver = :selenium_chrome_headless
- Handle Window Sizing: Because there is no physical screen, Chrome defaults to a very small window size which can cause elements to hide behind hamburger menus or overlap. Always include
options.add_argument('--window-size=1280,800')(or your preferred resolution) to ensure consistent element visibility. - Run Your Tests: Execute your test suite as usual. You will notice that no browser window opens, but your console will report the success or failure of the tests based on the headless interactions.
Common Mistakes
- Version Mismatch: The most frequent error occurs when the installed version of Google Chrome is updated, but the
chromedriveris outdated. Using thewebdriversgem usually automates this, but manual installations require a manual update. - Forgetting Window Size: Omitting the window size argument often leads to "element not clickable" or "element not found" errors because the browser renders in a tiny default window, triggering mobile layouts or hiding elements.
- Incorrect Driver Registration: Attempting to set
Capybara.current_driver = :headless_chromebefore actually registering that symbol viaregister_driverwill result in an unknown driver error. - Ignoring GPU Issues: On some Linux environments, failing to add
--disable-gpucan cause the browser to crash or hang during startup.
Tips
- Toggle Headless Mode: Use an environment variable to switch between headless and headed mode. This allows you to run tests headlessly in CI but visually on your local machine for debugging:
options.add_argument('--headless') unless ENV['HEADFUL'] - Take Screenshots on Failure: Since you cannot see the browser, use
page.save_screenshot('failure.png')in your rescue blocks or RSpecafter(:each)hooks to see exactly what the page looked like when a test failed. - Use Chrome Options for Stability: Adding
--no-sandboxand--disable-dev-shm-usageis often necessary when running inside Docker containers to prevent memory-related crashes.
Troubleshooting
If your tests are failing or the browser refuses to start, check the following:
- Check the Driver Path: If you aren't using the
webdriversgem, ensurechromedriveris in your system PATH. Runchromedriver --versionin your terminal to verify. - Inspect Console Logs: If the browser crashes silently, configure Selenium to capture Chrome logs. This can reveal if a specific flag is causing the crash.
- Verify Chrome Installation: Ensure Chrome is actually installed on the machine. On CI servers, you may need to add a step to install
google-chrome-stable. - Wait Times: Headless browsers can sometimes behave slightly faster or slower than headed ones. If you encounter flaky tests, increase your
Capybara.default_max_wait_time.
Frequently Asked Questions
Is headless Chrome faster than headed Chrome?
Yes, it is generally faster because the system does not have to spend resources rendering the graphical user interface (GUI) and managing window events.
Does headless Chrome support JavaScript?
Yes. Headless Chrome is a full instance of the Chrome browser; it executes JavaScript exactly as the headed version does.
Can I debug tests while running in headless mode?
Directly, no, as there is no UI to inspect. However, you can use screenshots, HTML dumps (page.html), or switch to headed mode using an environment variable to debug the issue visually.
Why do some elements only disappear in headless mode?
This is usually due to the default window size. Headless Chrome may start with a resolution that triggers a responsive mobile view, hiding desktop elements. Always explicitly set the window size.
Do I need a separate driver for headless mode?
No. You use the same chromedriver; you simply pass the --headless argument to the Chrome options during the driver registration process.