Skip to content

Playwright Test

Playwright Test is an end-to-end test runner that opens a web app in Chromium, Firefox, and WebKit browsers, clicks and types through it the way a user would, and reports which checks passed or failed. Its command is npx playwright test.

Microsoft maintains Playwright. The @playwright/test package bundles the runner with the Playwright browser automation library, so tests don’t need a separate assertion library. Test runners such as Vitest and Jest usually check single modules, and Playwright Test checks the running app from the user’s side. Its closest peer is Cypress, another end-to-end test runner.

Installation and Setup

Playwright’s docs recommend the init command, which adds Playwright Test to a new or existing project:

Language: Shell
$ npm init playwright@latest

It asks a few questions, installs @playwright/test as a dev dependency, writes a playwright.config.ts or playwright.config.js file and an example test, and downloads the browsers. Playwright 1.65 supports Node.js 22, 24, and 26. In a project that already lists the package, the install command downloads the browsers:

Language: Shell
$ npx playwright install

Key Features

  • Runs the same tests in Chromium, Firefox, and WebKit, each set up as a project in the config file.
  • Waits for an element to be visible and enabled before clicking or typing into it.
  • Retries assertions such as toBeVisible() until they pass or time out after five seconds by default.
  • Runs test files in parallel worker processes, in headless browsers unless --headed is passed.
  • Records tests from clicks in a browser with npx playwright codegen and steps through them in UI mode with --ui.

Usage

Say a shop’s home page has an Add to cart button that updates a cart counter. A file named tests/cart.spec.js checks that flow:

Language: JavaScript
import { expect, test } from "@playwright/test";

test("adds a product to the cart", async ({ page }) => {
  await page.goto("/");
  await page.getByRole("button", { name: "Add to cart" }).click();
  await expect(page.getByText("Cart (1)")).toBeVisible();
});

The playwright.config.js file sets the base URL, one project per browser, and a webServer command that starts the app before the tests run:

Language: JavaScript
import { defineConfig, devices } from "@playwright/test";

export default defineConfig({
  testDir: "./tests",
  use: { baseURL: "http://localhost:3000" },
  projects: [
    { name: "chromium", use: { ...devices["Desktop Chrome"] } },
    { name: "firefox", use: { ...devices["Desktop Firefox"] } },
    { name: "webkit", use: { ...devices["Desktop Safari"] } },
  ],
  webServer: {
    command: "node server.mjs",
    url: "http://localhost:3000",
  },
});

The test command runs every test once in each project:

Language: Shell
$ npx playwright test

Running 3 tests using 1 worker

  ✓  1 [chromium] › tests/cart.spec.js:3:1 › adds a product to the cart (272ms)
  ✓  2 [firefox] › tests/cart.spec.js:3:1 › adds a product to the cart (2.4s)
  ✓  3 [webkit] › tests/cart.spec.js:3:1 › adds a product to the cart (881ms)

  3 passed (8.0s)

The --project flag limits a run to one browser, and -g runs only the tests whose titles match a regular expression:

Language: Shell
$ npx playwright test --project webkit
$ npx playwright test -g "cart"

The --headed flag shows the browser windows while the tests run, and --last-failed reruns only the tests that failed in the previous run:

Language: Shell
$ npx playwright test --headed
$ npx playwright test --last-failed

For every command-line option, see the Playwright Test documentation.

A developer presses a Run button on a testing machine that scans skis and a ski boot, with panels labeled Test, Assert, Mock and Await.

Course

JavaScript Testing for Pythonistas

Port a ski-hire shop's pytest suite to JavaScript, and learn to test with Node's built-in runner: assertions, errors, many cases, shared setup, async code, and mocks.

intermediate py testing

For additional information on related topics, take a look at the following resources:

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Nov. 23, 2026