Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Automate browser interactions for web testing, form filling, screenshots, and data extraction using the playwright-cli tool.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/spec-driven-testing.md
1# Spec-driven testing (plan → generate → heal)23End-to-end workflow for authoring and maintaining Playwright tests using `playwright-cli`. The three sections below can be used independently:45- **Planning** — explore the app, produce a spec file describing what to test.6- **Generate** — turn a spec into Playwright test files. Update the spec if it's vague or stale.7- **Heal** — diagnose failing tests, fix the code, reconcile the spec with reality.89All three lean on the same mechanic: run `npx playwright test --debug=cli` in the background, then `playwright-cli attach tw-XXXX` to drive the paused page interactively. See [playwright-tests.md](playwright-tests.md) for the debug/attach mechanics and [test-generation.md](test-generation.md) for how every `playwright-cli` action emits Playwright TypeScript.1011---1213## 1. Planning1415Goal: produce a spec file (e.g. `specs/<feature>.plan.md`) that enumerates the scenarios to test. **Always** write the spec to a file.1617### 1.1 Prerequisite: workspace1819Check the workspace has Playwright installed before anything else:2021```bash22# Either of these confirms a workspace:23test -f playwright.config.ts || test -f playwright.config.js24npx --no-install playwright --version25```2627If there is no Playwright install, bootstrap one and let the user pick the defaults:2829```bash30npm init playwright@latest31```3233### 1.2 Prerequisite: seed test3435A **seed test** is a minimal test that lands the page in the state every scenario starts from: navigation to the app, any required login, feature flags, etc. Scenarios assume a fresh start *after* the seed. `--debug=cli` pauses *inside* this test, so the seed is where every planning and generation session begins.3637Minimum viable seed:3839```ts40// tests/seed.spec.ts41import { test } from '@playwright/test';4243test('seed', async ({ page }) => {44await page.goto('https://example.com/');45});46```4748Preferred — push navigation into a fixture so scenario tests reuse it:4950```ts51// tests/fixtures.ts52import { test as baseTest } from '@playwright/test';53export { expect } from '@playwright/test';5455export const test = baseTest.extend({56page: async ({ page }, use) => {57await page.goto('https://example.com/');58await use(page);59},60});61```6263```ts64// tests/seed.spec.ts65import { test } from './fixtures';6667test('seed', async ({ page }) => {68// Fixture already navigates. This empty body tells agents where to start.69});70```7172If no seed exists, create one that at least navigates to the app.7374### 1.3 Explore the app7576Launch the app via the seed in the background and attach:7778```bash79PLAYWRIGHT_HTML_OPEN=never npx playwright test tests/seed.spec.ts --debug=cli80# wait for "Debugging Instructions" and the session name tw-XXXX81playwright-cli attach tw-XXXX82```8384Resume so the seed runs, then probe the app:8586```bash87playwright-cli resume # resume so that seed test runs fully88playwright-cli snapshot # inventory of interactive elements89playwright-cli click e5 # follow a flow90playwright-cli eval "location.href" # read URL / state91playwright-cli show --annotate # ask the user to point at something92```9394Map out:9596- Interactive surfaces (forms, buttons, lists, filters, modals).97- Primary user journeys end-to-end.98- Edge cases: empty states, validation errors, very long input, boundary values.99- Persistence: reload, local/session storage, URL fragments.100- Navigation: which controls change the URL, back/forward behaviour.101102**Important**: Do not just open the app url with playwright-cli, always go through the test to capture any custom setup done there.103**Important**: Stop the background test when done exploring.104105### 1.4 Write the spec file106107Save under `specs/<feature>.plan.md`. Use this structure:108109```markdown110# <Feature> Test Plan111112## Application Overview113114<One paragraph describing what the feature does and why it matters.>115116## Test Scenarios117118### 1. <Group Name>119120**Seed:** `tests/seed.spec.ts`121122#### 1.1. <kebab-case-scenario-name>123124**File:** `tests/<group>/<kebab-case-scenario-name>.spec.ts`125126**Steps:**1271. <Concrete user step>128- expect: <observable outcome>129- expect: <another observable outcome>1302. <Next step>131- expect: <outcome>132133#### 1.2. <next-scenario>134...135136### 2. <Next Group>137138**Seed:** `tests/seed.spec.ts`139...140```141142Guidelines:143144- Each scenario is independent and starts from the seed's fresh state — never chain scenarios.145- Scenario names are kebab-case and match the test file name (`should-add-single-todo` → `should-add-single-todo.spec.ts`).146- Cover happy path, edge cases, validation, negative flows, persistence.147- Write steps at the user level ("Type 'Buy milk' into the input"), not the API level ("call `fill`").148- Put observable outcomes in `- expect:` bullets; each becomes an assertion during generation.149150---151152## 2. Generate153154Goal: take a spec file and produce Playwright test files. Optionally update the spec if it has drifted.155156### 2.1 Inputs157158- **Spec file**, e.g. `specs/basic-operations.plan.md`.159- **Target**: either a single scenario (e.g. `1.2`), a whole group (`1`), or all.160- **Seed file**, read from the `**Seed:**` line of the scenario's group.161162### 2.2 Generate one scenario163164For each target scenario, in sequence (never in parallel — scenarios share the seed session):165166```bash167PLAYWRIGHT_HTML_OPEN=never npx playwright test <seed-file> --debug=cli # background168playwright-cli attach tw-XXXX169# resume170```171172**Do not** just open the app url with playwright-cli, always go through the test to capture any custom setup done there.173174Walk the scenario's `Steps:` one by one with `playwright-cli`, treating the spec as the plan and the live app as the source of truth. If a step is vague ("click the button" — which button?), references an element that no longer exists, or contradicts the app's actual behaviour, use your judgement: update the spec to match what the app really does, then keep going. Editing the spec mid-generation is expected.175176Every action prints the equivalent Playwright TypeScript (see [test-generation.md](test-generation.md)):177178```bash179playwright-cli snapshot # find refs180playwright-cli fill e3 "John Doe" # -> page.getByRole('textbox', {...}).fill(...)181playwright-cli press Enter182playwright-cli click e7183```184185For each `- expect:` bullet, add an explicit assertion. See [test-generation.md](test-generation.md) for details.186187Collect the generated code and write the test file at the path given in the spec:188189```ts190// spec: specs/basic-operations.plan.md191// seed: tests/seed.spec.ts192import { test, expect } from './fixtures'; // or '@playwright/test' if no fixtures file193194test.describe('Singing in and out', () => {195test('should sign in', async ({ page }) => {196// 1. Navigate to the application197// (handled by the seed fixture)198199// 2. Type 'John Doe' into the username field200await page.getByRole('textbox', { name: 'username' }).fill('John Doe');201202// 3. Type password203await page.getByRole('textbox', { name: 'password' }).fill('TestPassword');204205// 4. Press Enter to submit206await page.getByRole('textbox', { name: 'password' }).press('Enter');207208await expect(page.getByRole('heading')).toContainText('Welcome, John Doe!');209});210});211```212213Rules:214215- **One test per file.** File path, describe name, and test name come verbatim from the spec (minus the ordinal).216- Prefix each numbered step with a `// N. <step text>` comment before its actions.217- Use the describe group name verbatim from the spec (no `1.` ordinal).218- Import from `./fixtures` if the project has one; otherwise `@playwright/test`.219- **Important**: close the CLI session and stop the background test before moving to the next scenario.220221### 2.3 Generate multiple scenarios222223Loop 2.2 over the targeted scenarios one at a time, restarting the seed between each so every test starts from a clean page. This is safe to parallelise due to unique generated session names - just make sure each test run is stopped.224225### 2.4 Run generated tests226227After generation, run the new tests once:228229```bash230PLAYWRIGHT_HTML_OPEN=never npx playwright test tests/<group>/<scenario>.spec.ts231```232233Any failure goes to Section 3.234235---236237## 3. Heal238239Goal: fix failing tests, and update the spec if the app's intended behaviour changed.240241### 3.1 Find failing tests242243```bash244PLAYWRIGHT_HTML_OPEN=never npx playwright test245```246247Record the list of failing `<file>:<line>` entries and process them one at a time. Do not attempt parallel fixes — shared state and the single CLI session make that fragile.248249### 3.2 Debug one failure250251Run the single failing test in debug mode in the background, then attach:252253```bash254PLAYWRIGHT_HTML_OPEN=never npx playwright test tests/<group>/<scenario>.spec.ts:<line> --debug=cli255# wait for "Debugging Instructions" and the tw-XXXX session name256playwright-cli attach tw-XXXX257```258259The test is paused at the start. Step forward or run to until just before the failing action or assertion, then diagnose:260261```bash262playwright-cli snapshot # did the element change / move / rename?263playwright-cli console # app-side errors?264playwright-cli network # failed request? wrong payload?265playwright-cli show --annotate # ask the user to point somewhere266```267268Common causes: selector drift, new wrapper element, label/ARIA rename, timing (transition, async load), assertion text updated in the app, test data leaking between runs.269270Rehearse the corrected interaction with `playwright-cli` — the generated code in the output is what you paste back into the test.271272### 3.3 Apply the fix273274Edit the test file: update the locator, assertion, step order, or inputs to match the corrected behaviour. Stop the background debug run. Rerun the single test to confirm green.275276Never skip hooks or add sleeps as a fix. Never use `networkidle`.277278### 3.4 Reconcile with the spec279280Open the spec referenced by the `// spec:` header in the test file and locate the scenario that matches the test.281282- **Fix was purely technical** (locator drift, better assertion shape) and the spec's user-level behaviour still matches the app → leave the spec alone.283- **Fix changed user-visible steps, inputs, order, or expected outcomes** that the spec describes → update the spec to match reality. Keep the scenario id and file path stable; only the step / expect lines change.284- **Unclear whether the app change is intentional** (spec is stale) **or a regression** (test was right, app is wrong) → **stop and ask the user**. Provide:285- the scenario id (e.g. `2.3`),286- the spec lines that no longer match,287- the observed app behaviour (quote a snapshot excerpt or a concrete outcome).288289Only after the user answers, either update the spec (intentional change) or file/flag the test as covering a bug (regression).290291### 3.5 Iteration and giving up292293- Fix failures one at a time; rerun after each.294- If after thorough investigation you are confident the test is correct but the app is wrong *and* the user has confirmed it's a bug: mark the test `test.fixme(...)` with a comment pointing at the user's decision or issue link. Never silently skip.295296---297298## Cross-references299300| For... | See |301|---|---|302| `--debug=cli` / attach mechanics | [playwright-tests.md](playwright-tests.md) |303| How `playwright-cli` actions become TS | [test-generation.md](test-generation.md) |304| Mocking requests during exploration/generation | [request-mocking.md](request-mocking.md) |305| Managing the CLI browser session | [session-management.md](session-management.md) |306