Run Browser Tests in Headless Mode: Playwright & Cypress
In this article
Running browser tests in headless mode means executing your test suite without a visible browser window. This is faster, uses fewer system resources, and is the standard approach for CI/CD pipelines. Both Playwright and Cypress support headless execution with simple command-line flags or configuration changes. Below is a step-by-step guide for both tools.
What You'll Need
- Node.js (v16 or later for Playwright, v18 or later for Cypress)
- A code editor
- Basic familiarity with JavaScript/TypeScript
- An existing test project using either Playwright or Cypress (or start fresh)
Step-by-Step: Playwright
- Install Playwright (if not already installed):
npm init playwright@latestornpm install @playwright/test. This creates aplaywright.config.jsfile. - Run tests in headless mode: By default, Playwright runs headless. Simply execute
npx playwright test. To be explicit, usenpx playwright test --headed=false. - Configure headless mode in the config file: Open
playwright.config.js. Underuse, setheadless: true. For example:module.exports = { use: { headless: true, // other options }, }; - Run a specific test file headlessly:
npx playwright test tests/login.spec.js– it will run headless by default. - Use environment variables for flexibility: In your config, set
headless: process.env.HEADLESS !== 'false'. Then runHEADLESS=false npx playwright testto see the browser, andnpx playwright testfor headless.
Step-by-Step: Cypress
- Install Cypress (if not already):
npm install cypress --save-dev. Then open Cypress once to initialize:npx cypress open– this creates thecypressfolder. - Run tests headlessly: Use
npx cypress run. This runs all tests in headless mode by default. To run a specific file:npx cypress run --spec "cypress/e2e/login.cy.js". - Choose a browser for headless mode: By default, Cypress uses Electron. To use Chrome headless:
npx cypress run --browser chrome. For Firefox:--browser firefox. - Configure headless via cypress.config.js: Set
defaultCommandTimeoutor other options, but headless is not a config flag – it's the default forcypress run. To force headed mode, usenpx cypress open. - Run in CI with headless mode: In your CI pipeline (e.g., GitHub Actions), use
npx cypress run– it will run headless automatically. You can also add--headlessflag explicitly, though it's redundant.
Common Mistakes
- Forgetting that Playwright's default is headless – so adding
--headedis needed to see the browser, not the other way around. - In Cypress, using
cypress openin CI – this launches the interactive runner and hangs. Always usecypress runin CI. - Not specifying a browser in Cypress headless – Electron works, but Chrome is closer to production. Use
--browser chrome. - Assuming headless mode behaves identically to headed – some animations or viewport sizes may differ. Test both if possible.
- Ignoring the
--projectflag in Playwright when you have multiple browser projects – you might be running only one browser unintentionally.
Tips
- Use
--reporter=listin Playwright for clearer output in CI. - In Cypress, use
--recordif you have a Cypress Cloud account to get video recordings of headless runs. - Set
video: truein Playwright config to capture videos of failed tests – useful for debugging headless failures. - For Cypress, set
video: trueincypress.config.jsto record test runs. - Run headless tests locally before pushing to CI to catch environment-specific issues.
Troubleshooting
- Tests pass headed but fail headless: Check for timing issues – headless is faster, so waits may be insufficient. Add explicit waits or increase timeouts. Also check viewport size – set
viewportin config to match headed mode. - Cypress hangs in headless mode: Ensure you're using
cypress run, notcypress open. Also check for unclosed resources like intervals or websockets. - Playwright cannot find browser binaries: Run
npx playwright installto download the required browsers. - Cypress says "Cypress detected that you have a project that uses the `cypress run` command, but you haven't set a `baseUrl`": Set
baseUrlincypress.config.jsor useCYPRESS_BASE_URLenvironment variable. - Headless mode doesn't support certain features: For example, some browser extensions or media codecs may not work. Use a different browser or run headed for those specific tests.
Frequently Asked Questions
Does headless mode run faster than headed?
Generally yes, because it skips rendering to a screen, but the difference is often small. The main benefit is stability in CI environments.
Can I run Playwright tests in headless mode on Windows?
Yes, Playwright supports headless mode on Windows, macOS, and Linux.
How do I take screenshots in headless mode?
Both tools support screenshots in headless mode. In Playwright, use page.screenshot(); in Cypress, use cy.screenshot().
Can I use headless mode for debugging?
It's harder to debug without a visible browser, but you can use Playwright's --debug flag or Cypress's --headed for debugging. Alternatively, use trace viewers or video recordings.
Do I need to install separate browsers for headless mode?
No, both tools use the same browser binaries for headed and headless. Just ensure they are installed (Playwright: npx playwright install; Cypress: it bundles its own Electron, but for Chrome you need Chrome installed).
Can I run headless tests in parallel?
Yes, both tools support parallel execution. In Playwright, use workers config; in Cypress, use --parallel with Cypress Cloud.