Fix Cypress in CircleCI Browser Connection Timeout
In this article
When Cypress times out connecting to the browser in CircleCI, it usually means the browser process failed to launch or the WebSocket handshake between Cypress and the browser couldn't complete. This guide walks you through the most common fixes: ensuring the right dependencies are installed, using the correct CircleCI executor, and adjusting Cypress configuration for headless environments.
What You'll Need
- A CircleCI project with a
.circleci/config.ymlfile - A Cypress test suite (version 10 or later recommended)
- Basic familiarity with YAML and CircleCI configuration
Step-by-Step
- Use the official Cypress CircleCI Orb. The Cypress orb handles browser dependencies and caching automatically. In your
config.yml, addorbs: cypress: cypress-io/cypress@3and replace your job withcypress/run. This ensures the correct system libraries are installed for Chrome and Electron. - Switch to a machine executor if you're using a Docker executor. The
machineexecutor provides a full VM with more resources and fewer container restrictions. Change your job tomachine: trueor usemachine: image: ubuntu-2004:2024.01.1. This often resolves browser launch issues caused by missing kernel features in Docker. - Install all required system dependencies. If you must use a Docker executor, ensure your image includes the libraries Cypress needs. The Cypress orb's default image (
cypress/base) already has them. If you're using a custom image, add a step to installlibgtk-3,libnotify,libnss3,libxss1,libasound2, andxvfb. For example, on Ubuntu:sudo apt-get update && sudo apt-get install -y libgtk-3-0 libnotify4 libnss3 libxss1 libasound2 xvfb. - Increase the Cypress browser launch timeout. In your
cypress.config.jsorcypress.json, add"defaultCommandTimeout": 10000and"pageLoadTimeout": 60000. This gives the browser more time to connect, especially on slower CI machines. - Disable the Cypress video recording if you don't need it. Video recording can add overhead and cause timeouts. Set
video: falsein your Cypress config or use the--record falseflag in the command line. - Run Cypress with the
--browser chromeflag. Sometimes Electron fails to launch in CI, but Chrome works. In your CircleCI config, change the run command tonpx cypress run --browser chrome. - Add
xvfbif you're not using the orb. If you're running Cypress directly on a machine executor, you may need to start a virtual framebuffer. Add a step:xvfb-run -a npx cypress run. - Cache the Cypress binary. Use the orb's built-in caching or add a
save_cache/restore_cachestep for~/.cache/Cypress. This prevents re-downloading the binary, which can cause timeouts if the download is slow. - Check your CircleCI resource class. If you're using a small resource class (e.g.,
medium), upgrade tolargeorxlarge. Insufficient memory can cause the browser to crash or hang. - Set the
CYPRESS_CACHE_FOLDERenvironment variable. If you're using a custom cache path, ensure it's writable. Addenvironment: CYPRESS_CACHE_FOLDER: ~/.cache/Cypressto your job.
Common Mistakes
- Using a Docker executor without the Cypress base image. Many custom images lack browser dependencies.
- Forgetting to set
xvfbwhen running on a machine executor without a display. - Not increasing timeouts; default Cypress timeouts are often too short for CI.
- Using
--browser electronwhen Chrome is more stable in CI. - Ignoring the Cypress orb's built-in caching, leading to slow binary downloads that time out.
Tips
- Always use the latest version of the Cypress orb (check the orb registry).
- Run your tests locally in a Docker container first to reproduce the issue.
- Add a
startcommand to your Cypress config to ensure your app is served before tests run. - Use
cypress run --headedlocally to see if the browser launches at all. - Consider using
cypress/browsersorb for more browser-specific dependencies.
Troubleshooting
If you still see timeouts, try these steps in order:
- Check the CircleCI logs. Look for the exact error message. If it says "Timed out waiting for the browser to connect," it's a launch issue. If it says "WebSocket connection failed," it's a network or proxy issue.
- Enable verbose Cypress logging. Add
DEBUG=cypress:server:launcher,cypress:server:browserto your environment variables to see detailed browser launch output. - Try a different browser. If Chrome fails, try Firefox with
--browser firefox. If Electron fails, try Chrome. - Test with a minimal spec. Create a simple test that just visits a page to isolate the issue from your actual tests.
- Check your CircleCI network settings. If you're behind a corporate proxy, set the
HTTP_PROXYandHTTPS_PROXYenvironment variables. - Upgrade your CircleCI image. If using
circleci/node, switch to a newer tag or use the Cypress base image.
Frequently Asked Questions
Why does Cypress time out only in CircleCI but not locally?
CI environments often have fewer system resources, missing dependencies, or run headless. The browser may not launch due to missing libraries or a virtual display.
Do I need to install Chrome separately?
No, Cypress bundles its own Electron browser. However, if you want to use Chrome, you need to install it in your CI image or use the Cypress orb which includes it.
Can I use a Docker executor with the Cypress orb?
Yes, the Cypress orb works with Docker executors, but it's recommended to use the cypress/base image or the orb's default executor to avoid dependency issues.
What is the recommended CircleCI resource class for Cypress?
At least medium+ (2 CPUs, 4GB RAM) is recommended. For larger suites, use large or xlarge.
How do I fix a WebSocket connection failure?
This is often caused by network restrictions. Ensure your CI environment allows WebSocket connections (port 443). If you're using a proxy, configure it correctly in your environment variables.
Should I use the Cypress orb or write my own config?
Using the orb is strongly recommended because it handles dependencies, caching, and parallelization. Writing your own config is possible but requires more maintenance.