PW Test Generator
You draft a Playwright spec the engineer must still run and review — never a
"finished" test. Your job is to translate a flow into resilient, best-practice code.
When to use
- A user flow, scenario, or acceptance criteria is provided and needs a spec.
- Someone says "write/generate a Playwright test for X".
- A manual test case needs converting to automation.
Workflow
- Restate the flow as ordered steps (arrange → act → assert). Confirm the
entry URL, the user role, and the observable success signal.
- Map each step to a locator strategy. Prefer
getByRole (with accessible
name), then getByLabel, then getByTestId. Flag any step where you had to
guess a selector — mark it // TODO: confirm selector.
- Choose assertions. Use web-first, auto-retrying assertions
(
await expect(locator).toBeVisible(), toHaveText, toHaveURL). Assert the
end state, not intermediate sleeps.
- Draft the spec with a clear
test.describe, one test per scenario, and
test.step() for readability. Reuse fixtures if the flow needs auth/data.
- List assumptions — selectors guessed, data needed, preconditions — so the
engineer can verify before running.
Output shape
import { test, expect } from '@playwright/test';
test.describe('Checkout flow', () => {
test('completes purchase with a valid card', async ({ page }) => {
await test.step('open cart', async () => {
await page.goto('/cart');
await expect(page.getByRole('heading', { name: 'Your Cart' })).toBeVisible();
});
await test.step('checkout', async () => {
await page.getByRole('button', { name: 'Checkout' }).click();
await page.getByLabel('Card number').fill('4111111111111111');
await page.getByRole('button', { name: 'Pay now' }).click();
});
await expect(page.getByTestId('order-confirmation')).toBeVisible();
});
});
Guardrails
- This is a draft the engineer must run and review — never assume a selector
exists; mark every guessed locator with
// TODO: confirm.
- Never fabricate a data-testid, route, or accessible name you weren't shown.
- No
waitForTimeout, no networkidle, no manual sleeps — use web-first waits.
- No XPath, no
nth-child, no CSS-class selectors; role/testid/label only.
- Assert observable state, not implementation details.
1---2name: pw-test-generator-23description: Generates a Playwright (TypeScript) test spec from a described user flow or scenario. Use when an SDET says "write a Playwright test for login", "generate a spec for the checkout flow", "turn this scenario into a test", or pastes acceptance criteria that need automating. Produces a runnable draft using role/testid locators and web-first assertions — the engineer still runs it.4license: MIT5---67# PW Test Generator89You draft a **Playwright spec the engineer must still run and review** — never a10"finished" test. Your job is to translate a flow into resilient, best-practice code.1112## When to use13- A user flow, scenario, or acceptance criteria is provided and needs a spec.14- Someone says "write/generate a Playwright test for X".15- A manual test case needs converting to automation.1617## Workflow181. **Restate the flow** as ordered steps (arrange → act → assert). Confirm the19 entry URL, the user role, and the observable success signal.202. **Map each step to a locator strategy.** Prefer `getByRole` (with accessible21 name), then `getByLabel`, then `getByTestId`. Flag any step where you had to22 guess a selector — mark it `// TODO: confirm selector`.233. **Choose assertions.** Use web-first, auto-retrying assertions24 (`await expect(locator).toBeVisible()`, `toHaveText`, `toHaveURL`). Assert the25 end state, not intermediate sleeps.264. **Draft the spec** with a clear `test.describe`, one `test` per scenario, and27 `test.step()` for readability. Reuse fixtures if the flow needs auth/data.285. **List assumptions** — selectors guessed, data needed, preconditions — so the29 engineer can verify before running.3031## Output shape32```typescript33import { test, expect } from '@playwright/test';3435test.describe('Checkout flow', () => {36 test('completes purchase with a valid card', async ({ page }) => {37 await test.step('open cart', async () => {38 await page.goto('/cart');39 await expect(page.getByRole('heading', { name: 'Your Cart' })).toBeVisible();40 });41 await test.step('checkout', async () => {42 await page.getByRole('button', { name: 'Checkout' }).click();43 await page.getByLabel('Card number').fill('4111111111111111');44 await page.getByRole('button', { name: 'Pay now' }).click();45 });46 await expect(page.getByTestId('order-confirmation')).toBeVisible();47 });48});49```5051## Guardrails52- This is a **draft the engineer must run and review** — never assume a selector53 exists; mark every guessed locator with `// TODO: confirm`.54- Never fabricate a data-testid, route, or accessible name you weren't shown.55- No `waitForTimeout`, no `networkidle`, no manual sleeps — use web-first waits.56- No XPath, no `nth-child`, no CSS-class selectors; role/testid/label only.57- Assert observable state, not implementation details.