Installation guide

Before installing Playwright, ensure your system meets the following requirements:

  • Node.js: Playwright requires Node.js version 12 or higher. You can download Node.js from https://nodejs.org/.
  • npm (Node Package Manager): npm is included with Node.js, which is used to install Playwright.
  • Python (Optional): If you plan to use Playwright’s code generation feature, Python 2.7 or 3.7+ is required.
  • Supported Operating System: Playwright supports Windows, macOS, and Linux. Ensure your operating system is up to date for the best compatibility.

In my Setting up my environment course I defined all kinds of checks you can do in order to validate if your environment is ready for testing

Installation Steps

  1. Open Terminal or Command Prompt: Navigate to your project directory or where you wish to install Playwright.
  2. Create a new Node.js project (if necessary): If you are starting a new project,

npm init -y

This command creates a new package.json file.

Install Playwright:

Run the following command to install Playwright and its browser binaries:


npm i playwright

This command installs Playwright along with Chromium, Firefox, and WebKit browsers.


npx playwright --version

This command displays the installed version of Playwright, indicating a successful installation.

Post-Installation Checks

  • Run a Sample Test: Create a simple test script to verify that Playwright can launch browsers and execute tests. Save the following code in a file named test.js:
const { chromium } = require('playwright');(async () => {
const browser = await chromium.launch({ headless: false })
const page = await browser.newPage();
 await page.goto('https://learnautomatedtesting.com');
 await page.screenshot({ path: `learnautomatedtesting.png` });
 await browser.close();

})();

Run the test using:


node test.js

This script opens the Playwright documentation page in a browser, takes a screenshot, and saves it as example.png

Check Browser Binaries: Verify that the browser binaries are installed correctly by checking the .cache directory in your Node.js project or the global cache location.