Skip to content

Playwright Setup

This lesson walks through installing Playwright in a Node.js project, downloading browser binaries, and understanding the configuration file generated on first init.

Installing Playwright

The fastest path is npm init playwright@latest, which scaffolds config, example tests, and a GitHub Actions workflow. Alternatively, add @playwright/test manually and run npx playwright install to download Chromium, Firefox, and WebKit.

Playwright bundles its own browser builds for consistency across machines — your local Chrome version does not need to match CI.

npm init playwright@latest
# or manually:
npm install -D @playwright/test
npx playwright install

playwright install downloads browser binaries. Use playwright install --with-deps on Linux CI for system libraries.

Common Setup Commands

npx playwright test              # run all tests headless
npx playwright test --ui         # interactive UI Mode
npx playwright test --headed     # show browser window
npx playwright test login.spec.ts
npx playwright show-report       # open HTML report
  • playwright test is the primary command for local runs and CI.
  • --ui opens UI Mode for time-travel debugging.
  • --project=chromium runs only tests configured for that browser project.
  • show-report opens the last HTML report after a run.

Setup Command Reference

Commands you will run while setting up and using Playwright locally.

Command Purpose
npm init playwright@latest Scaffold a new Playwright project
npx playwright install Download browser binaries
npx playwright install-deps Install OS dependencies (Linux CI)
npx playwright test Run all tests headlessly
npx playwright test --ui Launch UI Mode
npx playwright codegen <url> Record actions and generate locators
npx playwright show-trace trace.zip Open Trace Viewer

Understanding playwright.config.ts

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

export default defineConfig({
  testDir: './tests',
  fullyParallel: true,
  retries: process.env.CI ? 2 : 0,
  use: {
    baseURL: 'http://localhost:3000',
    trace: 'on-first-retry',
  },
  projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }],
});

Setting baseURL lets you write page.goto('/login') instead of full URLs.

Caching Browsers in CI

Browser binaries live under ~/.cache/ms-playwright. Cache this directory in CI alongside node_modules to avoid re-downloading on every pipeline run.

  • Run npx playwright install --with-deps in CI before tests.
  • Pin @playwright/test version in package.json for reproducible builds.
  • Use npx playwright install chromium if you only need one engine in CI.

Common Mistakes

  • Running tests without playwright install, causing 'Executable doesn't exist' errors.
  • Not installing system dependencies on Linux CI (install-deps).
  • Hardcoding full URLs everywhere instead of configuring baseURL.
  • Committing test-results/ or playwright-report/ instead of adding them to .gitignore.

Key Takeaways

  • npm init playwright@latest scaffolds a complete TypeScript project.
  • playwright install downloads consistent browser binaries for all environments.
  • playwright.config.ts centralizes base URL, retries, tracing, and projects.
  • Cache browser binaries in CI for faster pipelines.

Pro Tip

Set baseURL in config on day one. Switching between local, staging, and preview environments becomes a one-line env change instead of editing every spec.