User Agents

Web browsers and everyday web tech, explained

Cypress Headless Mode Extensions: Why They Fail and Fixes

· 6 min read · Extensions

In this article
  1. The Technical Root of the Problem
  2. How Browser Extensions Work in Automation
  3. Implementing the New Headless Mode
  4. Alternative Strategies for CI Environments
  5. Common Pitfalls and Configuration Errors
  6. Comparing Headless Approaches
  7. Frequently Asked Questions

Cypress cannot load Chrome extensions in headless mode because the Chromium engine disables the extension system entirely when the --headless flag is active. To resolve this, you must use the newer --headless=new flag (available in Chrome 109+) or run your tests in headed mode while using a CI tool that provides a virtual display like Xvfb.

The Technical Root of the Problem

To understand why extensions fail, it is necessary to understand how Chromium distinguishes between "headed" and "headless" environments. Traditionally, the --headless mode was a separate, stripped-down implementation of the browser designed for speed and minimal resource consumption. In this legacy mode, the browser skips the initialization of the browser UI, the profile manager, and the extension loading mechanism. Because extensions rely on the browser's UI shell and specific background page processes to function, they are simply not instantiated when the legacy headless flag is passed.

Cypress interacts with the browser via the Chrome DevTools Protocol (CDP). While Cypress can control the browser's actions, it cannot force the browser to enable a feature that the browser engine has explicitly disabled at the process level. When you launch Cypress in headless mode, it tells the browser to start without a window; Chromium interprets this as a command to optimize for performance by ignoring all .crx files and extension directories defined in the user profile.

How Browser Extensions Work in Automation

Chrome extensions operate in a multi-process architecture. They typically consist of a background script (service worker in Manifest V3), content scripts that inject into web pages, and a popup UI. For an extension to work during a Cypress test, three things must happen: the extension must be loaded into the browser profile, the background process must start, and the content scripts must be injected into the DOM of the application under test.

In a standard headed Cypress run, you can use the chromeWebSecurity: false setting in cypress.config.js to allow the browser to load local extension files. However, the transition to headless mode breaks this chain. Even if the files are present in the profile directory, the Chromium process refuses to execute the background scripts. This creates a gap where your application may be running, but the extension's logic—such as modifying headers, injecting UI elements, or intercepting network requests—is completely absent.

Implementing the New Headless Mode

Google introduced a "new" headless mode that is essentially the full Chrome browser running without a visible window, rather than a separate, stripped-down engine. This version supports extensions because it utilizes the same code path as the headed browser.

To implement this in Cypress, you need to modify the setupNodeEvents function in your configuration file to pass the specific flag to the browser. Here is the process:

  1. Open your cypress.config.js or cypress.config.ts file.
  2. Locate the setupNodeEvents block.
  3. Use the on('before:browser:launch', (browser = {}, launchOptions) => { ... }) hook.
  4. Check if the browser is Chrome or Chromium.
  5. Push '--headless=new' into the launchOptions.args array.
  6. Return the modified launchOptions object.

By using --headless=new, you are telling Chromium to run the full browser stack. This allows the extension loading mechanism to trigger, enabling your --load-extension paths to function as they would in a visible window. This is the most efficient way to maintain the speed of headless execution while retaining the ability to test extension-dependent features.

Alternative Strategies for CI Environments

If the new headless mode is unavailable or behaves inconsistently in your specific environment, the industry standard is to use a virtual frame buffer. Xvfb (X Virtual Frame Buffer) is a display server that performs all graphical operations in memory without showing any screen output. This tricks the browser into thinking it is running in headed mode, which ensures all extensions are loaded.

When using Xvfb in a CI pipeline (such as GitHub Actions, GitLab CI, or Jenkins), the workflow looks like this:

  • Install Xvfb on the runner machine (e.g., sudo apt-get install xvfb).
  • Wrap the Cypress execution command with the Xvfb wrapper: xvfb-run npx cypress run.
  • Ensure the browser is launched in headed mode (do not use the --headless flag).

This approach is often more stable than the --headless=new flag for complex extensions that rely heavily on window dimensions or specific UI events, as it provides a fully simulated desktop environment. The trade-off is a slight increase in memory usage on the CI runner, but it eliminates the "missing extension" problem entirely.

Common Pitfalls and Configuration Errors

Even with the correct flags, developers often encounter issues where extensions still do not appear. One common misconception is that simply adding the extension to the browser is enough. In automation, you must explicitly tell the browser where the unpacked extension folder is located using the --load-extension argument.

Another frequent problem is the conflict between chromeWebSecurity and extension permissions. If your extension modifies the DOM of a site with a strict Content Security Policy (CSP), Cypress may block the extension's scripts. Setting chromeWebSecurity: false in the config is essential, but you must also ensure that the extension itself has the necessary permissions in its manifest.json to interact with the target URL.

Furthermore, remember that extensions in automation often require a clean profile. If you are loading an extension from a local path, ensure the path is absolute. Relative paths often fail in CI environments because the working directory of the Cypress process differs from the directory where the browser is launched.

Comparing Headless Approaches

Choosing the right method depends on your specific testing goals and infrastructure. The legacy --headless mode is the fastest but is unsuitable for extension testing. The --headless=new mode offers a middle ground, providing near-native speed with full feature support. Xvfb is the most compatible but requires the most setup.

For most modern projects, the hierarchy of choice should be:

  • Primary Choice: --headless=new. It is the native Chrome solution and requires minimal configuration.
  • Secondary Choice: Xvfb. Use this if the extension interacts with the browser's actual window state or if --headless=new causes crashes in your CI image.
  • Last Resort: Running headed tests on a dedicated machine with a VNC server, which allows for real-time monitoring but is costly to maintain.

Frequently Asked Questions

Can I use the Chrome Web Store ID to load extensions in headless mode?

No, Cypress and Chromium automation cannot automatically download and install extensions from the Web Store via a URL or ID. You must download the extension's .crx file, unpack it into a folder, and use the --load-extension flag pointing to that local directory.

Will --headless=new work on all versions of Chrome?

The --headless=new flag was introduced in Chrome 109. If you are using an older version of the browser, this flag will be ignored, and the browser will revert to the legacy headless mode where extensions are disabled. Always ensure your browser version is updated in your CI environment.

Does disabling chromeWebSecurity affect how extensions load?

Yes. Setting chromeWebSecurity: false allows Cypress to bypass certain Same-Origin Policy restrictions. This is often necessary for extensions to inject scripts into pages or for the browser to load the extension's local files without triggering security warnings that would stop the test.

Why does my extension work in headed mode but fail in --headless=new?

This usually happens because some extensions rely on specific window events, such as onResize or visibility changes, which behave differently in a windowless environment. If your extension's logic depends on the actual rendering of a window, Xvfb is the only reliable solution.

Can I test the extension's popup menu in headless mode?

Testing the actual popup UI (the window that appears when clicking the extension icon) is extremely difficult in any headless mode because that UI exists outside the DOM of the web page. To test popup logic, it is better to test the underlying JavaScript functions or use a headed browser with a tool like Playwright or Puppeteer that has deeper access to browser internals.