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 need | Version / notes |
|---|---|
| Node.js | Current LTS release |
| A web app | Running locally or on a staging URL |
| Disk space | About 1 GB for the three browsers |
3. Local setup
1Create the Playwright setup
The initializer asks for TypeScript/JavaScript, the tests folder and whether to add a GitHub Actions workflow, then downloads browsers.
BASHnpm init playwright@latest2Run the example tests
Runs headless in all configured browsers in parallel.
BASHnpx playwright test npx playwright show-report
4. Create & run a project
1Write a test
Use user-facing locators (
getByRole,getByLabel) and web-first assertions that wait automatically.tests/login.spec.tsimport { 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(); });2Start your app automatically
webServerlaunches the dev server before tests andbaseURLlets you use relative URLs.playwright.config.tsimport { 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 }, });3Record a test with codegen
Click through your app; Playwright writes the code.
BASHnpx playwright codegen http://localhost:51734Debug with UI mode and traces
UI mode shows each step with DOM snapshots; traces record failures in CI.
BASHnpx 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.
BASE_URL=http://localhost:5173
E2E_USER=demo@example.com
E2E_PASSWORD=secret123 # test account onlyKey dependencies
| Package / tool | Used for |
|---|---|
@playwright/test | Test runner + assertions |
Browsers | Chromium, Firefox, WebKit (npx playwright install) |
Trace viewer / UI mode | Debugging tools |
HTML reporter | Report with screenshots, videos, traces |
playwright (Python/Java/.NET) | Same engine for other languages |
Folder structure
project/
├── tests/
│ ├── login.spec.ts
│ └── home.spec.ts
├── playwright.config.ts
├── playwright-report/ # HTML report
└── test-results/ # traces, screenshotsEssential commands
| Command | What it does |
|---|---|
npx playwright test | Run all tests |
npx playwright test --ui | Interactive UI mode |
npx playwright test --project=chromium | One browser |
npx playwright codegen URL | Record actions |
npx playwright show-report | Open the HTML report |
npx playwright install --with-deps | Install 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
| Requirement | Recommendation |
|---|---|
| CI runner | Linux runner; browsers installed with --with-deps |
| App under test | Started by webServer or a deployed preview/staging URL |
| Artifacts | HTML report and traces uploaded on failure |
| Gate | Required check before merging/deploying to production |
Deployment steps
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.ymlname: 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 test2Upload 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: 143Run smoke tests against staging after deploy
Point the same tests at the deployed URL with an environment variable.
BASHBASE_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.
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
waitForTimeoutfor synchronisation. - Each test sets up its own data/state (or reuse auth state files).
- Keep a small
@smokeset for post-deploy checks. - Collect traces on retry and review failures before re-running.