Testing & Code Quality

Playwright Setup & Deployment Guide

Reliable end-to-end testing in Chromium, Firefox and WebKit – auto-waiting, codegen, traces and HTML reports, running in CI before deploys.

Updated Official documentation ↗

1. Introduction & use cases

Playwright is an end-to-end testing framework from Microsoft that drives real browsers – Chromium, Firefox and WebKit (Safari’s engine) – from one API. It auto-waits for elements to be ready, which removes most flaky sleep calls, and isolates every test in a fresh browser context.

It comes with a test runner, parallel execution, mobile emulation, network mocking, codegen (record tests by clicking), a UI mode, trace viewer and HTML reports. Tests can be written in TypeScript/JavaScript, Python, Java or .NET.

Where it is used

  • End-to-end tests of login, checkout and other critical flows
  • Cross-browser and mobile-viewport testing
  • Visual regression with screenshots
  • Smoke tests against staging after each deploy

2. Prerequisites

You needVersion / notes
Node.jsCurrent LTS release
A web appRunning locally or on a staging URL
Disk spaceAbout 1 GB for the three browsers

3. Local setup

  1. 1Create the Playwright setup

    The initializer asks for TypeScript/JavaScript, the tests folder and whether to add a GitHub Actions workflow, then downloads browsers.

    BASH
    npm init playwright@latest
  2. 2Run the example tests

    Runs headless in all configured browsers in parallel.

    BASH
    npx playwright test
    npx playwright show-report

4. Create & run a project

  1. 1Write a test

    Use user-facing locators (getByRole, getByLabel) and web-first assertions that wait automatically.

    tests/login.spec.ts
    import { test, expect } from '@playwright/test';
    
    test('user can log in', async ({ page }) => {
      await page.goto('/login');
      await page.getByLabel('Email').fill('demo@example.com');
      await page.getByLabel('Password').fill('secret123');
      await page.getByRole('button', { name: 'Log in' }).click();
      await expect(page).toHaveURL(/dashboard/);
      await expect(page.getByRole('heading', { name: 'Welcome' })).toBeVisible();
    });
  2. 2Start your app automatically

    webServer launches the dev server before tests and baseURL lets you use relative URLs.

    playwright.config.ts
    import { defineConfig, devices } from '@playwright/test';
    
    export default defineConfig({
      testDir: './tests',
      retries: process.env.CI ? 2 : 0,
      use: { baseURL: 'http://localhost:5173', trace: 'on-first-retry' },
      projects: [
        { name: 'chromium', use: { ...devices['Desktop Chrome'] } },
        { name: 'mobile', use: { ...devices['Pixel 7'] } },
      ],
      webServer: { command: 'npm run dev', url: 'http://localhost:5173', reuseExistingServer: !process.env.CI },
    });
  3. 3Record a test with codegen

    Click through your app; Playwright writes the code.

    BASH
    npx playwright codegen http://localhost:5173
  4. 4Debug with UI mode and traces

    UI mode shows each step with DOM snapshots; traces record failures in CI.

    BASH
    npx playwright test --ui
    npx playwright test --headed --project=chromium
    npx playwright show-trace test-results/**/trace.zip

✅ When it works you should see output like Figure 1.

5. Development configuration

Environment variables

Keep test credentials and target URLs in environment variables so the same tests run against local, staging or preview deployments.

.env
BASE_URL=http://localhost:5173
E2E_USER=demo@example.com
E2E_PASSWORD=secret123   # test account only

Key dependencies

Package / toolUsed for
@playwright/testTest runner + assertions
BrowsersChromium, Firefox, WebKit (npx playwright install)
Trace viewer / UI modeDebugging tools
HTML reporterReport with screenshots, videos, traces
playwright (Python/Java/.NET)Same engine for other languages

Folder structure

project structure
project/
├── tests/
│   ├── login.spec.ts
│   └── home.spec.ts
├── playwright.config.ts
├── playwright-report/   # HTML report
└── test-results/        # traces, screenshots

Essential commands

CommandWhat it does
npx playwright testRun all tests
npx playwright test --uiInteractive UI mode
npx playwright test --project=chromiumOne browser
npx playwright codegen URLRecord actions
npx playwright show-reportOpen the HTML report
npx playwright install --with-depsInstall browsers + OS libraries (CI)

6. Production deployment

Test frameworks are not deployed to a production server – in production they act as a quality gate: they run automatically in CI on every push and pull request, publish reports, and block the deployment when a test fails. Playwright is also ideal for smoke tests against staging right after a deployment.

Server & hosting requirements

RequirementRecommendation
CI runnerLinux runner; browsers installed with --with-deps
App under testStarted by webServer or a deployed preview/staging URL
ArtifactsHTML report and traces uploaded on failure
GateRequired check before merging/deploying to production

Deployment steps

  1. 1Run the tests in CI on every push

    Tests protect production only if they run automatically. This GitHub Actions workflow installs dependencies and runs the suite on every push and pull request; mark the job as a required check in branch protection so failing code cannot be merged or deployed. Playwright needs its browsers and system libraries on the runner.

    .github/workflows/tests.yml
    name: tests
    on:
      push:
        branches: [main]
      pull_request:
    jobs:
      test:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v4
          - uses: actions/setup-node@v4
            with:
              node-version: lts/*
              cache: npm
          - run: npm ci
          - run: npx playwright install --with-deps
          - run: npx playwright test
  2. 2Upload the report

    Keep the HTML report (with traces) for failed runs.

    YAML
          - uses: actions/upload-artifact@v4
            if: ${{ !cancelled() }}
            with:
              name: playwright-report
              path: playwright-report/
              retention-days: 14
  3. 3Run smoke tests against staging after deploy

    Point the same tests at the deployed URL with an environment variable.

    BASH
    BASE_URL=https://staging.example.com npx playwright test --grep @smoke

🗺 The whole path from your code to the live site is drawn in the deployment diagram.

7. Images & screenshots

Illustrations drawn for this guide from the commands above. Product dashboards change often, so for screens such as cloud consoles follow the step text and the official documentation.

terminal$ npx playwright testRunning 6 tests using 3 workers ✓ 1 [chromium] › login.spec.ts:3:5 › user can log in (1.4s) ✓ 2 [mobile] › login.spec.ts:3:5 › user can log in (1.7s) ✓ 3 [chromium] › home.spec.ts:3:5 › has title (612ms) ... 6 passed (5.2s)To open last HTML report run: npx playwright show-report
Figure 1: terminal output of the sample Playwright project (section 4).
1Write specs2Local run + UImode3CI: installbrowsers4Tests + reportartifact5Smoke tests onstaging
Figure 2: production deployment flow (section 6).
project/├── tests/│ ├── login.spec.ts│ └── home.spec.ts├── playwright.config.ts├── playwright-report/ # HTML report└── test-results/ # traces, screenshots
Figure 3: project folder structure (section 5).

8. Common errors & solutions

browserType.launch: Executable doesn’t exist

Browsers are not installed for this Playwright version. Run npx playwright install (with --with-deps on Linux/CI).

Timeout 30000ms exceeded waiting for a locator

The element text/role is different or hidden. Use the UI mode/trace to inspect, prefer getByRole with the accessible name.

Strict mode violation: locator resolved to 2 elements

Make the locator more specific (getByRole(…, { name }), .filter(), or .first() if truly intended).

Flaky tests in CI only

Avoid fixed waits; use web-first assertions; check test isolation and data; use traces from retries to see the difference.

Missing system libraries on Linux

Install with npx playwright install-deps or use the official Playwright Docker image.

Best practices

  • Use role/label/text locators, not CSS paths.
  • Never use waitForTimeout for synchronisation.
  • Each test sets up its own data/state (or reuse auth state files).
  • Keep a small @smoke set for post-deploy checks.
  • Collect traces on retry and review failures before re-running.