Appearance
Playwright how-to
Browser e2e tests against the Next dev server (this monorepo uses port 7777).
Install
bash
pnpm add -D @playwright/test
pnpm exec playwright install chromium
# or: pnpm run test:e2e:install| Item | Detail |
|---|---|
| Config | playwright.config.ts |
| Specs | e2e/**/*.spec.ts |
| Base URL | app dev URL (here: http://localhost:7777) |
Commands
bash
pnpm run test:e2e
pnpm run test:e2e:ui
pnpm run test:e2e:codegen # start `pnpm run dev` first
pnpm exec playwright test e2e/<feature>/<file>.spec.tsImplementation examples
Config with webServer
ts
import { defineConfig } from '@playwright/test';
export default defineConfig({
testDir: './e2e',
use: { baseURL: 'http://localhost:7777' },
webServer: {
command: 'pnpm run dev',
url: 'http://localhost:7777',
reuseExistingServer: !process.env.CI,
},
});Role-based spec
ts
import { test, expect } from '@playwright/test';
test('home shows main landmark', async ({ page }) => {
await page.goto('/');
await expect(page.getByRole('main')).toBeVisible();
});Mock a mutating API
ts
await page.route('**/api/contact-enquiry', async route => {
await route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({ ok: true }),
});
});Codegen
bash
pnpm run dev
pnpm run test:e2e:codegen
# clean up to getByRole / labels before commitUnit vs e2e
| Layer | Tool | Use for |
|---|---|---|
| Unit | Vitest | Pure logic |
| E2E | Playwright | Navigation, a11y, forms, redirects |