What are the benefits of headless browser testing?
A headless browser is a real browser engine running without a visible window. It still parses HTML, runs JavaScript, applies CSS and lays out the page; it just never paints that page to your screen. Playwright runs headless by default, and for automated suites that is usually the right choice. Here is why, plus the cases where you should switch the window back on.
The benefits
1. It runs where there is no screen
CI runners, Docker containers and cloud VMs normally have no display. A headed browser there needs a virtual display such as Xvfb. Headless mode needs nothing extra, which is why a fresh GitHub Actions or Jenkins agent can run npx playwright test straight away. See integrating Playwright with CI/CD.
2. Fewer resources per browser
Without a window to draw, each browser instance generally uses less CPU and memory. How much less depends on the app and the machine, so measure your own suite rather than trusting a number from a blog. On a modest laptop or a small CI machine, this often decides how many workers you can run.
3. More parallel workers
Lower cost per browser means more tests at once. Playwright runs test files in parallel across workers, and headless browsers make that practical on a single machine. See parallelism in Playwright.
4. No interference with your work
Headed runs open windows that steal focus while you type. Headless runs stay in the background, so you can keep working while the suite runs locally.
5. Same engine, same assertions
The engine is the same family as the headed browser, so locators, auto-waiting, network routing, screenshots and traces all work the same. The difference between the two modes is covered in headful vs headless mode.
Switching modes
// playwright.config.ts
import { defineConfig } from '@playwright/test';
export default defineConfig({
use: {
headless: true, // the default; set false to always see the window
},
});
npx playwright test # headless
npx playwright test --headed # show the browser windows
npx playwright test --ui # UI mode: watch, pick and re-run tests
npx playwright test --debug # headed, with the Inspector, one step at a time
The trade-offs
- You cannot watch the failure. Make up for it with artefacts:
trace: 'on-first-retry',screenshot: 'only-on-failure'and video. See tracing and screenshots. - Small differences can appear. In Chromium, Playwright's headless mode uses a separate lightweight build by default. Its user agent says
HeadlessChrome, which some sites treat differently. If a test passes headed but fails headless, compare viewport, fonts and user agent first. Settingchannel: 'chromium'switches to Chromium's newer headless mode, which is closer to the normal browser. - Bot checks notice automation. Headless or not,
navigator.webdriveristruein an automated browser. Test environments should switch off CAPTCHAs instead of trying to hide automation. - Visual details need care. Screenshot baselines differ between operating systems and font sets, not because of headless mode itself, but because CI machines are usually Linux. Generate baselines where the tests run.
A sensible routine
- Write and debug a new test with
--uior--headed. - Run the full suite headless, locally and in CI.
- When a CI run fails, open the trace instead of re-running headed.
Previous question in this series: browser isolation in Playwright.
Want to master Playwright hands-on?
Join Uncodemy's Playwright Automation Testing Course in Noida — Dedicated Placement Support.
Explore the Course →