skills/playwright-e2e-testing/SKILL.md
Write and maintain Playwright end-to-end tests for web apps. Use when the user asks for browser or E2E coverage, or for tests covering pages, routes, redirects, navigation, dialogs, authentication, or multi-step user flows, even if they do not explicitly mention Playwright. Also use for API mocking, fixtures, and Playwright-specific assertions.
npx skillsauth add perdolique/workflow playwright-e2e-testingInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
This skill provides patterns and conventions for writing Playwright E2E tests for web applications, including SPA-specific techniques for routing, API mocking, and async navigation.
When setting up or modifying playwright.config.ts, see references/configuration.md for conventions on web server setup, reporters, traces, screenshots, and CI behavior.
If the project has a custom fixture file that extends Playwright's test object (common for API interception, shared setup, etc.), always import test and expect from that fixture — not from @playwright/test directly. Check the project's test directory for a fixtures/ folder or global.fixtures.ts.
// ✅ If project has custom fixtures — import from there
import { test, expect } from '../fixtures/global.fixtures.ts';
// ❌ Bypasses any project-level request interception or setup
import { test, expect } from '@playwright/test';
Standalone types are always fine to import directly:
import type { Page, Locator } from '@playwright/test';
Organize tests by feature domain. Each domain typically has its own fixture file:
tests/playwright/
├── constants.ts # Base URLs, shared constants
├── fixtures/
│ ├── global.fixtures.ts # Extended test/expect (if project uses one)
│ ├── core-api.fixtures.ts # Shared API mock helpers
│ └── {feature}.fixtures.ts # Feature-specific test data
├── {feature-domain}/
│ └── {feature}.test.ts # Test file
└── routing/
└── redirects.test.ts # Router/redirect tests
import type { Page } from '@playwright/test';
import { test, expect } from '../fixtures/global.fixtures.ts';
import { mockEndpoint } from '../fixtures/api.fixtures.ts';
import { baseFixture, variantFixture } from '../fixtures/{feature}.fixtures.ts';
// Helper functions (navigation, assertions)
async function openFeaturePage(page: Page): Promise<void> {
await page.goto('/feature?id=test-id');
}
test.describe('Feature name', () => {
test('description of expected behavior', async ({ page }) => {
await mockEndpoint(page, baseFixture);
await openFeaturePage(page);
await expect(page.getByText('Expected text')).toBeVisible();
});
});
Define helper functions at the top of the test file for repeated actions. Helpers improve readability and reduce duplication:
openDialog(page), gotoProductPage(page)expectSummary(page, options), expectAvailability(page, from, to)setupCommonMocks(page) for test-specific mock bundlesactionButtons(page) returning a LocatorWhen all tests in a describe block share identical mock setup:
test.describe('Router redirects', () => {
test.beforeEach(async ({ page }) => {
await setupCommonMocks(page);
});
test('redirects when resource is not found', async ({ page }) => {
await page.goto('/resource?id=unknown');
await expect(page).toHaveURL(/\/error\?id=unknown$/u);
});
});
For complex user flows (purchases, form submissions), use test.step() blocks. Playwright reports show which step failed, making debugging much faster.
test('completes purchase flow', async ({ page }) => {
await test.step('prepare mocks', async () => {
await mockProduct(page, productFixture);
await mockCheckout(page);
});
const purchaseRequestPromise = page.waitForRequest(
request => request.url().includes('/purchases') && request.method() === 'POST'
);
await test.step('open product page', async () => {
await gotoProductPage(page);
});
await test.step('select option', async () => {
await page.getByRole('button', { name: '14:00' }).click();
});
await test.step('verify confirmation page', async () => {
await expect.poll(() => new URL(page.url()).pathname).toBe('/order-confirmation');
});
await test.step('submit order', async () => {
await page.getByRole('button', { name: 'Confirm' }).click();
});
await test.step('verify purchase request', async () => {
const request = await purchaseRequestPromise;
expect(request.postDataJSON()).toEqual({
items: [{ id: 'product-1', date: '2088-04-21T11:00:00.000Z' }]
});
});
});
Key patterns in multi-step flows:
page.waitForRequest() — Set up BEFORE the action that triggers the requestexpect.poll() — Wait for async URL changes after SPA navigationsatisfies — Type-check request payload expectations without losing literal typesTo set localStorage values before the page loads (e.g., saved state for routing tests):
async function setSavedState(page: Page, key: string, value: unknown): Promise<void> {
await page.addInitScript(
({ storageKey, data }: { storageKey: string; data: string }) => {
localStorage.setItem(storageKey, data);
},
{ storageKey: key, data: JSON.stringify(value) }
);
}
addInitScript runs before the page loads, so the app reads the correct localStorage values during initialization. page.evaluate runs after the page loads, which is too late for route guards.
Prefer user-facing selectors in this order:
page.getByRole('button', { name: 'Confirm' }) — Accessible role + namepage.getByText('Expected text') — Visible text contentpage.getByTestId('action-button') — data-testid attributepage.getByText('text', { exact: true }) — Exact match to avoid partial hitspage.getByText(/regex pattern/u) — Regex for dynamic contentUse .first() when multiple identical elements exist on the page (e.g., text duplicated for mobile/desktop viewports).
// Visibility
await expect(page.getByText('Welcome')).toBeVisible();
// Element count
await expect(page.getByRole('button', { name: /\d{2}:\d{2}/u })).toHaveCount(3);
await expect(page.getByText('Not present')).toHaveCount(0); // Assert absence
// URL matching
await expect(page).toHaveURL('http://localhost:5050/error?id=unknown');
await expect(page).toHaveURL(/\/order-success\?id=test-id$/u);
// Async URL change (SPA navigation)
await expect.poll(() => new URL(page.url()).pathname).toBe('/order-confirmation');
// Interactive state
await expect(page.getByRole('button', { name: 'Submit' })).toBeEnabled();
await expect(page.getByRole('button', { name: 'Submit' })).toBeDisabled();
// Regex with unicode flag (for apostrophes, special chars)
await expect(page.getByText(/^You.re all set!$/u)).toBeVisible();
Always use the /u (unicode) flag on regex patterns to correctly handle special characters.
async function mockProducts(page: Page, response: unknown): Promise<void> {
await page.route('**/api/products**', async (route) => {
await route.fulfill({ json: response });
});
}
async function mockPurchase(page: Page, purchaseId: string, response: unknown): Promise<void> {
// Mock POST (create)
await page.route('**/api/purchases', async (route) => {
await route.fulfill({ json: { id: purchaseId } });
});
// Mock GET (status poll)
await page.route(`**/api/purchases/${purchaseId}`, async (route) => {
await route.fulfill({ json: response });
});
}
A common pattern is to block all external requests and only allow requests to the app itself:
await page.route('**/*', async (route) => {
const url = new URL(route.request().url());
if (url.origin === appBaseUrl) {
await route.continue();
} else {
await route.abort();
}
});
Playwright uses last-registered-wins for route matching. Register a new route for the same pattern to override an earlier mock:
// Global fixture mocks /api/properties with defaults
// Your test overrides with custom data:
await page.route('**/api/properties/**', async (route) => {
await route.fulfill({ json: customPropertyData });
});
async function setupExternalMocks(page: Page): Promise<void> {
await page.route('https://analytics.example.com/**', async (route) => {
await route.fulfill({ status: 200, body: '' });
});
await page.route('https://cdn.example.com/**', async (route) => {
await route.fulfill({ status: 200, body: '', contentType: 'image/jpeg' });
});
}
See references/fixtures.md for detailed patterns.
Use a base const object + spread for variants. Each test gets exactly the data shape it needs, and you can see what differs from the base at a glance:
export const itemBase = {
id: 'test-item-id',
name: 'Test Item',
status: 'active',
items: []
} as const;
export const itemWithProducts = {
...itemBase,
items: [{
productId: 'test-product-id',
status: 'confirmed'
}]
} as const;
When fixtures need many permutations with computed fields:
function createAccessKey(options: {
readonly id: string;
readonly type: 'code' | 'remote';
readonly name: string;
readonly code?: string | null;
}) {
return {
id: options.id,
type: options.type,
name: options.name,
code: options.code ?? null,
validFrom: '2024-01-01T00:00:00.000Z',
validTo: '2099-12-31T23:59:59.000Z'
} as const;
}
Factory functions stay private to the fixture file. Exported variants compose them.
| Convention | Why |
| --- | --- |
| Use as const on every exported object | TypeScript narrows the type, catches typos |
| Use far-future dates in fixtures (e.g., year 2088) | Won't expire during test lifetime |
| Use clearly fake IDs with consistent prefixes | Easy to grep, obviously not real data |
| Spread from base, override only what matters | Makes test intent clear |
For testing server-side API endpoints directly, use Playwright's APIRequestContext — no browser needed.
Use worker-scoped fixtures instead of beforeAll + shared mutable let variables. Worker scope creates the context once per worker thread (same performance), eliminates shared mutable state, and integrates cleanly with Playwright's teardown lifecycle.
// tests/playwright/fixtures.ts
import { test as base, type APIRequestContext } from '@playwright/test'
import { appBaseUrl } from './constants'
interface WorkerFixtures {
authedRequest: APIRequestContext;
}
export const test = base.extend<Record<never, never>, WorkerFixtures>({
authedRequest: [
async ({ playwright }, use) => {
const request = await playwright.request.newContext({ baseURL: appBaseUrl })
await request.post('/api/auth/create-session')
await use(request)
await request.dispose()
},
{ scope: 'worker' },
],
})
Import test from this file in API test files. The fixture is available as { authedRequest } in the test callback.
APIResponse.json() returns Promise<any> (Playwright's Serializable = any). Assigning any directly to a typed variable triggers no-unsafe-assignment. Use unknown as the intermediate type, then parse with a validation library:
// ✅ Correct — breaks out of any safely
const raw: unknown = await response.json()
const body = v.parse(mySchema, raw) // Valibot accepts unknown, returns typed result
// ❌ Wrong — casting any → specific type bypasses runtime check
const body = await response.json() as MyResponseType
// ❌ Wrong — no-await-expression-member: don't chain .json() onto an await
const raw: unknown = await (await request.get('/api/items')).json()
When using Valibot, split request and parse onto separate lines — Valibot's parse throws a descriptive error if the shape doesn't match, which makes test failures easy to diagnose.
When a validation schema already enforces response shape, shape assertions add zero value — the schema throws before assertions are even reached. Focus on assertions TypeScript and schemas can't verify:
// ❌ Redundant when v.parse(schema, raw) already validates the shape
expect(body).toMatchObject({ id: expect.any(Number), name: expect.any(String) })
// ✅ Tests actual behavior — HTTP contract, correct data, filter logic, auth
expect(response.status()).toBe(200)
expect(body.name).toBe('MSR')
expect(body.items.length).toBeGreaterThan(0)
expect(filteredBody.items.every(item => item.category.slug === 'sleeping-pads')).toBe(true)
High-value API assertions: status codes, specific values (names, slugs, IDs confirming correct record), filter correctness, pagination boundaries, auth enforcement (401 without session).
Create an anonymous context inline — never reuse the authenticated fixture for negative auth tests:
test('returns 401 without session cookie', async ({ playwright }) => {
const anonRequest = await playwright.request.newContext({ baseURL: appBaseUrl })
const response = await anonRequest.get('/api/equipment/groups')
expect(response.status()).toBe(401)
await anonRequest.dispose()
})
# Run all Playwright tests
npx playwright test
# Run a specific test file
npx playwright test tests/playwright/product/checkout.test.ts
# Run with UI mode for debugging
npx playwright test --ui
# Run with headed browser
npx playwright test --headed
# Run with Playwright inspector/debug mode
npx playwright test --debug
# Open the last HTML report
npx playwright show-report
Check the project's package.json for available test scripts — many projects define shortcuts like test:playwright, test:e2e, or similar.
Before considering an E2E test complete, verify:
test and expect imported from the project's fixture file (if one exists)@playwright/test (e.g., type Page, type Locator)as const on all exported objectstest.describe() groups related teststest.step() blockspage.waitForRequest() set up BEFORE the triggering action/u flag.first() used when multiple matching elements existconst raw: unknown = await response.json() — never assign any directlytoMatchObject assertions removedbeforeAll + let)development
Vue 3 + TypeScript component conventions for `.vue` SFC work. Use for Vue UI tasks that change component APIs/templates/styling/accessibility/composables/template refs/v-model or related component behavior. For Nuxt/Pinia/routing/E2E/Vitest tasks apply only to component-layer code and combine with the more specific local skill.
tools
Create or draft GitHub releases from existing tags and repository history. Use this whenever the user asks to publish a GitHub release, create release notes for a new version, mirror previous GitHub releases, release a tag/version, or says they have already released a new package version and need the matching GitHub release.
development
Plan and drive non-trivial coding work from ambiguous request to scoped implementation and verification. Use when the user asks to plan before coding, plan then implement, split work into iterations or PR-sized tasks, tackle a risky multi-file feature, refactor, migration, or recover after failed work. Do not use for simple one-step edits, commit or PR creation, pure framework/domain conventions, or repo-specific roadmap docs where a more specific planning skill applies.
development
TypeScript coding conventions for writing, reviewing, and refactoring typed code. Use when working on `.ts`, `.tsx`, or files that embed TypeScript such as Vue, Astro, or Svelte components. Also use for TypeScript snippets, typed refactors, and review comments about code organization or function structure.