Skip
AgentSandbox.host

Browser testing at scale with Playwright and Selenium: setup in Python and Node.js, Docker images, Selenium Grid, parallel runs, sharding and flaky tests.

Home + Log

Playwright testing with Node.js: install, first test, config

Answer first

To start Playwright testing in Node.js, run npm init playwright@latest, which adds @playwright/test, a playwright.config.ts and an example test, then run npx playwright test. Tests use the page fixture, role-based locators and retrying assertions; npx playwright show-report opens the results.

Writer
Published
Updated
min
3

Playwright Test is Playwright's own test runner for Node.js, published as the npm package @playwright/test. It bundles the runner, the browser fixtures and web-first assertions, so a new project needs nothing else. The current release when we checked was 1.65.1 (npm: @playwright/test, read 2026-11-26).

What do I need before I start?

You need a supported Node.js and operating system. Playwright's installation page lists "Node.js: latest 22.x, 24.x or 26.x", and Windows 11+, Windows Server 2019+ or WSL, macOS 14 (Sonoma) or later, and Debian 12 / 13 or Ubuntu 22.04 / 24.04 / 26.04 on x86-64 or arm64 (Playwright installation, read 2026-11-26).

How do I install Playwright Test?

Install Playwright Test with its project generator, then let it download the browsers.

  1. In your project folder, run the generator. The npm form is below; yarn create playwright and pnpm create playwright do the same (Playwright installation, read 2026-11-26).
npm init playwright@latest
  1. Answer its questions: TypeScript or JavaScript, the name of the tests folder, whether to add a CI workflow, and whether to install the browsers now. Re-running it later "does not overwrite existing tests".
  2. Check what it created: playwright.config.ts, and a tests folder holding example.spec.ts, a minimal example test.

To add Playwright to an existing project by hand instead:

npm install -D @playwright/test
npx playwright install --with-deps

The second command downloads the browser builds and, on Linux, the system packages they need (Playwright command line, read 2026-11-26).

How do I write my first test?

Write a test with the test function, take the page fixture, and assert with expect. This is Playwright's first example, saved as tests/example.spec.ts (Playwright writing tests, read 2026-11-26):

import { test, expect } from '@playwright/test';

test('has title', async ({ page }) => {
  await page.goto('https://playwright.dev/');

  // Expect a title "to contain" a substring.
  await expect(page).toHaveTitle(/Playwright/);
});

test('get started link', async ({ page }) => {
  await page.goto('https://playwright.dev/');

  // Click the get started link.
  await page.getByRole('link', { name: 'Get started' }).click();

  // Expects page to have a heading with the name of Installation.
  await expect(page.getByRole('heading', { name: 'Installation' })).toBeVisible();
});

We ran this file with @playwright/test 1.65.1 and Chromium on 26 November 2026, and both tests passed. What makes it reliable:

  • Isolation. "Pages are isolated between tests due to the Browser Context, which is equivalent to a brand new browser profile" (Playwright writing tests, read 2026-11-26).
  • Auto-waiting. Before a click, Playwright checks that the element is visible, stable, receives events and is enabled (Playwright actionability, read 2026-11-26).
  • Retrying assertions. toHaveTitle and toBeVisible keep checking until they pass; "By default, the timeout for assertions is set to 5 seconds" (Playwright assertions, read 2026-11-26).

The actions you will use most are click, fill, check, uncheck, hover, focus, press, setInputFiles and selectOption. The assertions you will use most include toBeVisible, toHaveText, toContainText, toHaveValue, toHaveURL and toHaveCount (Playwright writing tests, read 2026-11-26).

What should playwright.config.ts contain?

The config file sets where tests live, how many run at once, retries, reporters and which browsers to use. A starting config with the options from Playwright's configuration guide (Playwright test configuration, read 2026-11-26):

import { defineConfig, devices } from '@playwright/test';

export default defineConfig({
  testDir: 'tests',
  fullyParallel: true,
  forbidOnly: !!process.env.CI,
  retries: process.env.CI ? 2 : 0,
  reporter: 'html',
  use: {
    baseURL: process.env.BASE_URL,
    trace: 'on-first-retry',
  },
  projects: [
    { name: 'chromium', use: { ...devices['Desktop Chrome'] } },
    { name: 'firefox', use: { ...devices['Desktop Firefox'] } },
    { name: 'webkit', use: { ...devices['Desktop Safari'] } },
  ],
});

Two defaults are worth knowing: workers "Defaults to half of the number of logical CPU cores", and each test has a timeout that "Defaults to 30 seconds" (Playwright TestConfig, read 2026-11-26). If your tests need your app running, add a webServer block with the command that starts it and the url to wait for; Playwright waits up to 60000 ms for it by default.

How do I run the tests?

Run npx playwright test. The commands for day-to-day work, from Playwright running tests (read 2026-11-26):

Table: Everyday Playwright Test commands

TaskCommand
Run every testnpx playwright test
Show the browsersnpx playwright test --headed
One browser projectnpx playwright test --project webkit
One filenpx playwright test landing-page.spec.ts
Tests whose title matchesnpx playwright test -g "add a todo item"
Only the tests that failed last timenpx playwright test --last-failed
Interactive UI modenpx playwright test --ui
Step through with the Inspectornpx playwright test --debug
Open the HTML reportnpx playwright show-report

Our Playwright CLI reference lists every option, and running browser tests in parallel covers workers and sharding.

How do I update Playwright?

Update the package and the browsers together. Playwright's commands are npm install -D @playwright/test@latest and then npx playwright install --with-deps; npx playwright --version shows which version you have (Playwright installation, read 2026-11-26).

What we read for this article

Q&A

  1. Which Node.js versions does Playwright support?

    Playwright's installation page lists the latest 22.x, 24.x and 26.x releases. The @playwright/test package itself (1.65.1 when we checked) declares Node.js 20 or newer as its engine.

  2. What is the difference between playwright and @playwright/test?

    The playwright package is Playwright Library, which gives the APIs to launch and drive browsers. The @playwright/test package is Playwright Test, which adds a managed test runner with fixtures and assertions. Install the library for scripts and @playwright/test for tests.

  3. How many tests does Playwright run at once?

    By default Playwright Test uses worker processes up to half the number of logical CPU cores. Set workers in the config or pass --workers to change it.

  4. How do I see the browser while a test runs?

    Run npx playwright test --headed, or npx playwright test --ui for the interactive UI mode, or npx playwright test --debug to step through with the Inspector.