User Agents

Web browsers and everyday web tech, explained

Headless Chrome vs Headed: Selenium Test Impact

· 5 min read · Browsers

In this article
  1. What Headless Mode Actually Changes
  2. Performance Differences: Speed and Resource Use
  3. Behavioral Differences That Break Tests
  4. Common Problems and How to Fix Them
  5. How to Choose Between Headless and Headed
  6. Best Practices for Reliable Tests
  7. Frequently Asked Questions

Headless Chrome runs Selenium tests faster and with lower resource usage than headed mode, but it can hide real browser rendering behaviors, making some tests pass in headless and fail in headed (or vice versa). The core difference is that headless Chrome skips the graphical user interface and some rendering steps, which changes how the browser executes JavaScript, paints elements, and reports element visibility.

What Headless Mode Actually Changes

Headless Chrome is a version of the Chrome browser that runs without a visible window. It was introduced in Chrome 59 (2017) and became more stable with Chrome 60. When you run Selenium tests in headless mode, the browser still parses HTML, executes JavaScript, and handles network requests, but it does not paint to a screen. This means:

  • No GPU acceleration for compositing (though software rendering is used).
  • Window size and viewport are still set, but the actual display is virtual.
  • Some APIs behave differently because there is no physical display (e.g., window.outerWidth may be 0).

In contrast, headed mode runs the full browser with a window, which is what most users see. This affects how the browser handles layout, painting, and certain JavaScript events that depend on visibility or focus.

Performance Differences: Speed and Resource Use

Headless mode is generally faster because it skips the overhead of rendering to a screen and reduces CPU/GPU work. In practice, Selenium tests in headless Chrome can run 10-30% faster than headed, especially for tests that involve many page loads or animations. However, the exact speed gain depends on the test complexity and the machine. Headless also uses less memory and CPU, which is why it is popular for CI/CD pipelines where multiple tests run in parallel.

But speed isn't the only metric. Headed mode can be slower because it waits for paint events and may trigger different JavaScript timings (e.g., requestAnimationFrame). Some tests that wait for element visibility might time out in headed mode if the element is below the fold and the page doesn't scroll automatically, whereas headless might report it as visible because it doesn't require actual paint.

Behavioral Differences That Break Tests

The most common issue is that headless Chrome has a different user agent string and may not support certain features that are only available in headed mode. For example, headless Chrome (before version 112) did not support the --headless=new mode that mimics the full browser. Even with the new headless mode, there are differences:

  • Element visibility: Elements that are off-screen or behind another element may be considered visible in headless because there is no actual viewport clipping.
  • Focus and keyboard events: Headless mode may not fire focus events the same way because there is no real window focus.
  • Media queries and responsive design: Headless uses a default viewport of 800x600 unless you set it explicitly, which can break responsive tests.
  • Downloads and file pickers: Headless cannot interact with native file dialogs; you need to use the --download.default_directory and handle downloads differently.

A concrete example: a test that clicks a button that opens a modal dialog. In headed mode, the modal might appear with an animation, and the test waits for it to be visible. In headless, the animation might be skipped or completed instantly, so the element is immediately visible, and the test passes. But if the test relies on the animation's timing (e.g., checking that the element is not visible before the animation ends), it will fail in headless.

Common Problems and How to Fix Them

When running Selenium tests in headless Chrome, you may encounter:

  • Element not clickable: Because headless may not scroll elements into view automatically. Fix by using element.scrollIntoView() or setting a larger window size.
  • User agent detection: Some websites serve different content to headless browsers. Override the user agent with --user-agent to match the headed version.
  • Missing browser extensions: Headless mode disables extensions by default, so tests that rely on extensions will fail.
  • Incorrect window size: Always set the window size explicitly using --window-size=1920,1080 or via Selenium's set_window_size().

To debug, run the same test in headed mode and compare the console logs. Also, use Chrome's headless screenshot feature to see what the page looks like at the point of failure.

How to Choose Between Headless and Headed

There is no universal rule; it depends on your testing goals. Use headless when:

  • You run tests in CI/CD and need speed and low resource usage.
  • You don't need to visually inspect the test execution.
  • You are testing logic, API calls, or backend integrations.

Use headed when:

  • You need to debug visually or record video of the test.
  • You are testing UI features that depend on actual rendering, like animations, hover states, or media queries.
  • You are testing features that require a real display, like WebGL or canvas rendering.

Many teams run a hybrid approach: smoke tests in headless for speed, and full regression in headed mode before release.

Best Practices for Reliable Tests

To minimize differences between headless and headed, follow these practices:

  1. Always set an explicit viewport size in your test setup.
  2. Use the same user agent string in both modes.
  3. Avoid relying on animations; disable them or use waits that don't depend on timing.
  4. Use Selenium's WebDriverWait for element visibility, but be aware that visibility can differ.
  5. Run your tests in both modes at least once to identify any discrepancies.
  6. If you must use headless, consider using the new headless mode (--headless=new) which is closer to headed behavior.

Frequently Asked Questions

Does headless Chrome still run JavaScript?

Yes, headless Chrome executes JavaScript exactly like headed mode. It just doesn't render to a screen. Most JavaScript behaviors are identical, but some APIs that depend on layout or paint may behave differently.

Why do my tests pass in headless but fail in headed?

This usually happens because the test relies on something that is different in headless, such as element visibility, focus, or timing. For example, an element might be considered visible in headless even if it's off-screen, while in headed mode it's not. Also, animations may complete instantly in headless, so waits for animation end may fail.

Can I take screenshots in headless mode?

Yes, you can take screenshots using Selenium's get_screenshot_as_file() or save_screenshot(). Headless Chrome supports screenshots, but the output will reflect the virtual viewport, not a real screen.

Is headless mode faster for all tests?

Not always. For tests that are CPU-bound or involve heavy JavaScript, headless may be faster. But for tests that wait for network responses or user interactions, the difference is minimal. The speed gain is most noticeable when running many tests in parallel.

Should I use headless for production testing?

It depends. If your users use a real browser, then headed mode is more realistic. Headless is best for CI and quick checks. For critical UI flows, always test in headed mode at least once.