User Agents

Web browsers and everyday web tech, explained

Fix Cypress in CircleCI Browser Connection Timeout

· 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

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.yml file
  • A Cypress test suite (version 10 or later recommended)
  • Basic familiarity with YAML and CircleCI configuration

Step-by-Step

  1. Use the official Cypress CircleCI Orb. The Cypress orb handles browser dependencies and caching automatically. In your config.yml, add orbs: cypress: cypress-io/cypress@3 and replace your job with cypress/run. This ensures the correct system libraries are installed for Chrome and Electron.
  2. Switch to a machine executor if you're using a Docker executor. The machine executor provides a full VM with more resources and fewer container restrictions. Change your job to machine: true or use machine: image: ubuntu-2004:2024.01.1. This often resolves browser launch issues caused by missing kernel features in Docker.
  3. 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 install libgtk-3, libnotify, libnss3, libxss1, libasound2, and xvfb. For example, on Ubuntu: sudo apt-get update && sudo apt-get install -y libgtk-3-0 libnotify4 libnss3 libxss1 libasound2 xvfb.
  4. Increase the Cypress browser launch timeout. In your cypress.config.js or cypress.json, add "defaultCommandTimeout": 10000 and "pageLoadTimeout": 60000. This gives the browser more time to connect, especially on slower CI machines.
  5. Disable the Cypress video recording if you don't need it. Video recording can add overhead and cause timeouts. Set video: false in your Cypress config or use the --record false flag in the command line.
  6. Run Cypress with the --browser chrome flag. Sometimes Electron fails to launch in CI, but Chrome works. In your CircleCI config, change the run command to npx cypress run --browser chrome.
  7. Add xvfb if 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.
  8. Cache the Cypress binary. Use the orb's built-in caching or add a save_cache/restore_cache step for ~/.cache/Cypress. This prevents re-downloading the binary, which can cause timeouts if the download is slow.
  9. Check your CircleCI resource class. If you're using a small resource class (e.g., medium), upgrade to large or xlarge. Insufficient memory can cause the browser to crash or hang.
  10. Set the CYPRESS_CACHE_FOLDER environment variable. If you're using a custom cache path, ensure it's writable. Add environment: CYPRESS_CACHE_FOLDER: ~/.cache/Cypress to your job.

Common Mistakes

  • Using a Docker executor without the Cypress base image. Many custom images lack browser dependencies.
  • Forgetting to set xvfb when running on a machine executor without a display.
  • Not increasing timeouts; default Cypress timeouts are often too short for CI.
  • Using --browser electron when 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 start command to your Cypress config to ensure your app is served before tests run.
  • Use cypress run --headed locally to see if the browser launches at all.
  • Consider using cypress/browsers orb 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:browser to 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_PROXY and HTTPS_PROXY environment 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.