User Agents

Web browsers and everyday web tech, explained

Run Chrome Tests in Headless Mode: A Complete Guide

· 5 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Running Chrome in headless mode allows you to execute browser-based tests and automation scripts without a visible graphical user interface (GUI). This is essential for Continuous Integration (CI) pipelines, server-side environments, and speeding up test execution by reducing the overhead required to render a window. By operating the browser in the background, developers can run thousands of tests across different scenarios without manually watching a browser window open and close on their desktop.

What You'll Need

  • Google Chrome: The latest stable version installed on your machine.
  • ChromeDriver: A standalone executable that matches your Chrome version to allow communication between your code and the browser.
  • A Programming Language Environment: Node.js (for Puppeteer or Playwright), Python (for Selenium), or Java.
  • A Test Framework: Such as Jest, PyTest, or JUnit.

Step-by-Step

  1. Install the Browser and Driver: Ensure Google Chrome is installed. Download the matching version of ChromeDriver from the official Chrome for Testing availability dashboard. Add the ChromeDriver executable to your system's PATH so your scripts can call it from any directory.
  2. Set Up Your Project Dependencies: Depending on your tool of choice, install the necessary libraries. For example, if using Python with Selenium, run pip install selenium. If using Node.js with Puppeteer, run npm install puppeteer.
  3. Configure the Browser Options: You must explicitly tell the browser to start without a GUI. This is done by adding specific arguments to the browser configuration object.
    • For Selenium (Python): Use ChromeOptions() and add the argument --headless=new.
    • For Puppeteer (Node.js): Pass headless: "new" (or true) into the puppeteer.launch() method.
    • For Playwright: Set headless: true in the browser launch options.
  4. Define a Window Size: Because there is no physical screen to determine the viewport, headless Chrome may default to a very small window size, which can cause elements to hide or overlap. Always set a explicit window size using the --window-size=1920,1080 argument or a setViewport command.
  5. Implement Your Test Script: Write your automation logic (e.g., navigating to a URL, clicking buttons, asserting text). Ensure you include a command to close the browser instance at the end of the script to prevent "zombie" Chrome processes from consuming system RAM.
  6. Execute the Tests: Run your script via the terminal. For example, python test_script.py or npm test. You will notice that no browser window appears, but the console will output the results of your assertions.

Common Mistakes

  • Using Outdated Headless Flags: Older versions of Chrome used --headless. Newer versions (Chrome 109+) introduced --headless=new, which behaves much more like a real browser. Using the old flag can lead to discrepancies where tests pass in headless mode but fail in headed mode.
  • Ignoring User-Agent Strings: Some websites detect the "HeadlessChrome" string in the User-Agent header and block the request to prevent scraping. This causes tests to fail with 403 Forbidden errors.
  • Forgetting to Close the Browser: If a test crashes before reaching the browser.close() command, the Chrome process remains active in the background. Over time, this can crash a CI server.
  • Assuming Default Viewports: Failing to set a screen resolution often leads to "Element Not Interactable" errors because the headless browser renders the page in a mobile-like narrow width by default.

Tips

  • Take Screenshots on Failure: Since you cannot see the browser, use a screenshot command within your error-handling block. This allows you to see exactly what the page looked like at the moment of failure.
  • Use a Custom User-Agent: Override the default headless User-Agent with a standard Chrome string to avoid being flagged as a bot by security filters.
  • Disable GPU Acceleration: In some Linux environments (especially Docker), adding the --disable-gpu flag prevents crashes related to hardware acceleration that isn't available on the server.
  • Implement Logging: Use detailed console logging to track the progress of the headless session, as you lack the visual feedback of the browser's progress bar.

Troubleshooting

If your tests are failing in headless mode but passing in headed mode, check the following:

  • Check for Timing Issues: Headless mode is often faster than headed mode. This can cause "race conditions" where the script tries to click an element before it has finished rendering. Increase your explicit waits.
  • Verify Resource Access: Ensure the user account running the tests has permissions to execute the ChromeDriver binary and access the temporary folders Chrome uses for user profiles.
  • Inspect the HTML Dump: When a test fails, print the current page source (HTML) to the console. This helps determine if the page failed to load or if the content shifted.
  • Check for Sandbox Issues: If running in a Linux container, you may need the --no-sandbox and --disable-dev-shm-usage flags to prevent the browser from crashing due to restricted shared memory.

Frequently Asked Questions

Is headless mode faster than headed mode?

Yes, it is generally faster because the system does not have to spend resources rendering the visual pixels of the browser window on the screen.

Does headless Chrome support JavaScript?

Yes, headless Chrome is a full browser engine. It executes JavaScript, handles CSS, and manages cookies exactly as the visible version does.

Can I debug a headless test?

Directly, no, because there is no UI. However, you can debug by taking screenshots, dumping the HTML source, or switching the headless flag to false temporarily to watch the execution.

Why do some websites block headless Chrome?

Websites use "bot detection" services that look for specific browser fingerprints, such as the navigator.webdriver property or the "HeadlessChrome" User-Agent string.

Do I need a different driver for headless mode?

No, you use the same ChromeDriver regardless of whether you are running in headed or headless mode; you only change the configuration arguments in your code.