User Agents

Web browsers and everyday web tech, explained

Run Browser Tests in Headless Mode: Playwright & Cypress

· 4 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step: Playwright
  3. Step-by-Step: Cypress
  4. Common Mistakes
  5. Tips
  6. Troubleshooting
  7. Frequently Asked Questions

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

  1. Install Playwright (if not already installed): npm init playwright@latest or npm install @playwright/test. This creates a playwright.config.js file.
  2. Run tests in headless mode: By default, Playwright runs headless. Simply execute npx playwright test. To be explicit, use npx playwright test --headed=false.
  3. Configure headless mode in the config file: Open playwright.config.js. Under use, set headless: true. For example:
    module.exports = {
      use: {
        headless: true,
        // other options
      },
    };
  4. Run a specific test file headlessly: npx playwright test tests/login.spec.js – it will run headless by default.
  5. Use environment variables for flexibility: In your config, set headless: process.env.HEADLESS !== 'false'. Then run HEADLESS=false npx playwright test to see the browser, and npx playwright test for headless.

Step-by-Step: Cypress

  1. Install Cypress (if not already): npm install cypress --save-dev. Then open Cypress once to initialize: npx cypress open – this creates the cypress folder.
  2. 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".
  3. Choose a browser for headless mode: By default, Cypress uses Electron. To use Chrome headless: npx cypress run --browser chrome. For Firefox: --browser firefox.
  4. Configure headless via cypress.config.js: Set defaultCommandTimeout or other options, but headless is not a config flag – it's the default for cypress run. To force headed mode, use npx cypress open.
  5. 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 --headless flag explicitly, though it's redundant.

Common Mistakes

  • Forgetting that Playwright's default is headless – so adding --headed is needed to see the browser, not the other way around.
  • In Cypress, using cypress open in CI – this launches the interactive runner and hangs. Always use cypress run in 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 --project flag in Playwright when you have multiple browser projects – you might be running only one browser unintentionally.

Tips

  • Use --reporter=list in Playwright for clearer output in CI.
  • In Cypress, use --record if you have a Cypress Cloud account to get video recordings of headless runs.
  • Set video: true in Playwright config to capture videos of failed tests – useful for debugging headless failures.
  • For Cypress, set video: true in cypress.config.js to 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 viewport in config to match headed mode.
  • Cypress hangs in headless mode: Ensure you're using cypress run, not cypress open. Also check for unclosed resources like intervals or websockets.
  • Playwright cannot find browser binaries: Run npx playwright install to 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 baseUrl in cypress.config.js or use CYPRESS_BASE_URL environment 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.