User Agents

Web browsers and everyday web tech, explained

Playwright Show Browser Window: How to Disable Headless

· 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

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

  1. 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(), or webkit.launch() is called.
  2. 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.
  3. 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 });
  4. 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 slowMo property (measured in milliseconds) to the launch options:
    const browser = await chromium.launch({ headless: false, slowMo: 100 });
  5. Run your script: Execute your test or script via the terminal using npx playwright test (for test runners) or node your-script.js (for library scripts). The browser window will now pop up and perform the actions automatically.
  6. 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 use object and add headless: false:
    export default defineConfig({
      use: {
        headless: false,
      },
    });

Common Mistakes

  • Forgetting the await keyword: Since launch() is an asynchronous function, failing to use await will result in the browser not opening correctly or the script crashing before the window appears.
  • Misplacing the headless flag: Placing headless: false inside the browser.newContext() or browser.newPage() methods instead of browser.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: false will 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 uses headless: 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 run HEADLESS=false npx playwright test from the command line.
  • Maximize Window: When running headed, the browser may open in a small default size. Use viewport: { width: 1920, height: 1080 } in the newContext() 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.ts file, remember that command-line flags often override the config file. Ensure you aren't passing a --headless flag 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 DISPLAY environment 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@latest followed by npx 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.