Playwright Windows Setup: Install Browsers and Run Tests
In this article
Running Playwright on Windows allows you to automate Chromium, Firefox, and WebKit browsers for end-to-end testing of web applications. This setup enables developers to write scripts in JavaScript, TypeScript, Python, or .NET to ensure their websites function correctly across different browser engines. To get started, you will need a code editor and a runtime environment installed on your machine.
What You'll Need
- Node.js: Version 16 or newer is required (LTS version recommended).
- Code Editor: Visual Studio Code (VS Code) is highly recommended for its dedicated Playwright extension.
- Terminal: Windows PowerShell, Command Prompt, or the integrated terminal in VS Code.
- Internet Connection: Required to download the Playwright framework and the specific browser binaries.
Step-by-Step
- Install Node.js: Download the installer from the official Node.js website. Run the .msi file and follow the prompts. Ensure the option "Add to PATH" is checked during installation so you can run
npmcommands from any terminal window. - Create a Project Directory: Open your terminal and create a new folder for your automation project. Navigate into it using the following commands:
mkdir playwright-testscd playwright-tests - Initialize Playwright: Run the automatic installation command to set up the project structure:
npm init playwright@latest
The terminal will prompt you with several questions. Choose whether you want to use TypeScript or JavaScript, where the tests should be located (default istests), and if you want to add a GitHub Actions workflow for CI/CD. - Install Browser Binaries: While the
initcommand usually handles this, if you ever need to manually install the required browsers (Chromium, Firefox, and WebKit), run:npx playwright install
This downloads the exact browser versions Playwright needs to ensure consistent test results. - Explore the Project Structure: Once installed, you will see a
playwright.config.ts(or .js) file. This is where you define global settings like the headful/headless mode, timeout limits, and which browsers to run. Thetestsfolder contains example test files. - Run Your First Test: Execute the default example tests to verify the installation is working correctly:
npx playwright test
By default, Playwright runs tests in "headless" mode, meaning no browser window will pop up. - View the Test Report: After the tests finish, Playwright generates a detailed HTML report. Open it by running:
npx playwright show-report - Run Tests in Headed Mode: To see the browser actually performing the actions, run the tests with the
--headedflag:npx playwright test --headed
Common Mistakes
- Skipping Browser Installation: Attempting to run
npx playwright testbefore runningnpx playwright installwill result in an error stating that browsers are missing. - Incorrect Node Version: Using an outdated version of Node.js can lead to installation failures or runtime errors during the
npm initprocess. - Permissions Issues: Running the terminal without administrative privileges can sometimes block the installation of browser binaries in protected system folders.
- Ignoring the Config File: Trying to change browser behavior (like viewport size) in every single test file instead of updating the
playwright.config.tsfile.
Tips
- Use the VS Code Extension: Install the "Playwright Test for VS Code" extension. It allows you to run tests by clicking a "Play" button next to the code and provides a powerful debugger.
- Codegen for Fast Scripting: Use the
npx playwright codegencommand. This opens a browser window that records your manual interactions and automatically generates the test code for you. - UI Mode: Use
npx playwright test --uito launch the UI Mode, which provides a time-travel debugger to see exactly what happened at each step of your test. - Selective Testing: Instead of running all tests, run a specific file using
npx playwright test tests/example.spec.ts.
Troubleshooting
If you encounter errors during setup, check the following solutions:
- "npm is not recognized as an internal or external command": This means Node.js is not in your system PATH. Restart your terminal or manually add the Node.js installation folder to your Environment Variables.
- Browser Crash on Windows: If browsers fail to launch, ensure your firewall or antivirus is not blocking the Playwright browser binaries. Check if you have enough disk space, as browser binaries are relatively large.
- Timeout Errors: If tests fail due to timeouts, increase the
timeoutvalue in theplaywright.config.tsfile or usepage.waitForSelector()to ensure elements are loaded before interacting. - Dependency Errors: If you see errors regarding missing system DLLs, run
npx playwright install-depsto install the necessary operating system dependencies for the browsers.
Frequently Asked Questions
Do I need to install Chrome or Firefox separately?
No. Playwright downloads its own modified versions of Chromium, Firefox, and WebKit. This ensures that the browser version is locked and consistent across all environments.
Can I run Playwright tests on a specific browser?
Yes. You can specify the browser in the config file or run a specific project from the CLI, for example: npx playwright test --project=chromium.
What is the difference between Headless and Headed mode?
Headless mode runs the browser in the background without a GUI, which is faster and ideal for CI/CD. Headed mode opens the browser window so you can visually watch the test execution.
Does Playwright support multiple tabs or windows?
Yes. Playwright provides a browserContext API that allows you to create multiple isolated browser contexts, each acting like a separate incognito window.
How do I update Playwright to the latest version?
Run npm install @playwright/test@latest and then run npx playwright install to update the browser binaries to match the new version.