Playwright Show Browser Window: How to Disable Headless
In this article
To show the browser window in Playwright, you must set the headless option to false within the browser launch configuration. By default, Playwright runs in "headless" mode, meaning the browser operates in the background without a graphical user interface (GUI) to maximize execution speed and reduce resource consumption. Disabling this mode allows you to visually observe the automation process, which is essential for debugging scripts, verifying UI elements, and understanding how a page renders in real-time.
What You'll Need
- Node.js installed on your machine.
- A project initialized with Playwright (via
npm init playwright@latest). - A code editor, such as Visual Studio Code.
- The Playwright browser binaries installed (via
npx playwright install).
Step-by-Step
-
Locate your browser launch code: Find the section of your script where the browser is initialized. In a basic script, this is typically where
chromium.launch(),firefox.launch(), orwebkit.launch()is called. -
Pass the options object: The
launch()method accepts an optional configuration object. If you are currently calling it without arguments (e.g.,await chromium.launch()), you need to add curly braces{}inside the parentheses. -
Set headless to false: Inside the options object, add the key-value pair
headless: false. Your code should look like this:const browser = await chromium.launch({ headless: false }); -
Add a slowMo delay (Optional but Recommended): When the browser window is visible, Playwright often executes commands faster than the human eye can follow. To slow down the execution for observation, add the
slowMoproperty (measured in milliseconds) to the launch options:const browser = await chromium.launch({ headless: false, slowMo: 100 }); -
Run your script: Execute your test or script via the terminal using
npx playwright test(for test runners) ornode your-script.js(for library scripts). The browser window will now pop up and perform the actions automatically. -
Configure via playwright.config.ts (For Test Runner): If you are using the Playwright Test runner instead of a standalone script, you can set this globally in the configuration file. Locate the
useobject and addheadless: false:export default defineConfig({ use: { headless: false, }, });
Common Mistakes
- Forgetting the await keyword: Since
launch()is an asynchronous function, failing to useawaitwill result in the browser not opening correctly or the script crashing before the window appears. - Misplacing the headless flag: Placing
headless: falseinside thebrowser.newContext()orbrowser.newPage()methods instead ofbrowser.launch(). The headless setting is a property of the browser instance, not the page or context. - Leaving headless: false in production: Deploying code to a CI/CD pipeline (like GitHub Actions or Jenkins) with
headless: falsewill cause the tests to fail because those environments typically lack a display server (X11 or Wayland). - Confusing headless with headed: Some users try to use a property called
headed: true. While some other frameworks use this terminology, Playwright specifically usesheadless: false.
Tips
- Use UI Mode: If you are using the Playwright Test runner, instead of just disabling headless mode, try running
npx playwright test --ui. This opens a full interactive suite where you can step through each action and see the browser state at every point. - Environment Variables: To avoid changing code every time you want to switch modes, use an environment variable. For example:
headless: process.env.HEADLESS === 'true'. This allows you to runHEADLESS=false npx playwright testfrom the command line. - Maximize Window: When running headed, the browser may open in a small default size. Use
viewport: { width: 1920, height: 1080 }in thenewContext()method to ensure the window is a standard size for visual inspection.
Troubleshooting
If the browser window still does not appear after setting headless: false, consider the following solutions:
- Check for Multiple Instances: Ensure there aren't zombie browser processes running in the background that are interfering with the new launch. Check your Task Manager or Activity Monitor and kill any remaining Chromium/Firefox processes.
- Verify Configuration Overrides: If you are using a
playwright.config.tsfile, remember that command-line flags often override the config file. Ensure you aren't passing a--headlessflag in your terminal command. - Check Display Drivers: On Linux systems, if you are running in a virtual environment or via SSH, you must have a window manager installed and the
DISPLAYenvironment variable set correctly, otherwise the browser cannot render a GUI. - Update Playwright: Ensure your Playwright version and browser binaries are in sync. Run
npm install @playwright/test@latestfollowed bynpx playwright install.
Frequently Asked Questions
Does disabling headless mode slow down the tests?
Yes, running in headed mode is generally slower because the system must allocate resources to render the graphical user interface and paint the pixels on the screen.
Can I switch between headless and headed mode without changing the code?
Yes, by using the Playwright Test runner, you can pass the --headed flag in the terminal: npx playwright test --headed.
Why does my browser window close immediately after finishing?
Playwright is designed to close the browser as soon as the script completes. To keep it open for inspection, you can add a long pause using await page.waitForTimeout(10000) or use the pause() command: await page.pause(), which opens the Playwright Inspector.
Does headless: false work on all browsers?
Yes, the headless option is supported across all Playwright-supported browsers, including Chromium, Firefox, and WebKit.
What is the difference between headed mode and UI mode?
Headed mode simply shows the browser window as it runs. UI mode is a comprehensive development tool that provides a time-travel debugger, a list of all tests, and a way to isolate and re-run specific tests visually.