Generate Playwright Tests

Generate Playwright tests.

How to use it

Claude Code
  1. Run the line below. It pulls the whole folder into ~/.claude/skills/generate.
  2. Describe your job in plain words. Claude Code follows the skill from there.
Claude Code — installs the whole folder, not just SKILL.md
npx degit alirezarezvani/claude-skills/engineering-team/playwright-pro/skills/generate#main ~/.claude/skills/generate

For one project only, change the path to .claude/skills/generate.

Claude (web or desktop app)
  1. On this page open ⋯ → Download .md.
  2. Save it as SKILL.md in a folder, zip the folder, then Customize → Skills → + → Create skill → Upload a skill.
  3. Pick the file and Save. Claude shows the name and description and runs a security scan.
  4. Check the skill is switched on.
  5. Start a new chat and describe your job in plain words. The AI follows the skill from there.
ChatGPT or another app
  1. ChatGPT: make a Project and paste it into Instructions.
  2. Neither? Paste it at the top of a new chat — it works for that chat.
Not working?
  • Check which app you pasted it into — the steps above name the right one.
  • Some skills need the paid tier of Claude or ChatGPT.
Step-by-step guide with screenshots · Ask in the forum

Paste into Claude, ChatGPT or Cursor.

Source of Generate Playwright Tests

Show the full text145 lines
namedescription
generate>- Generate Playwright tests. Use when user says "write tests", "generate tests", add tests for", "test this component", "e2e test", "create test for", test this page", or "test this feature".

Generate Playwright Tests

Generate production-ready Playwright tests from a user story, URL, component name, or feature description.

Input

$ARGUMENTS contains what to test. Examples:

  • "user can log in with email and password"
  • "the checkout flow"
  • "src/components/UserProfile.tsx"
  • "the search page with filters"

Steps

1. Understand the Target

Parse $ARGUMENTS to determine:

  • User story: Extract the behavior to verify
  • Component path: Read the component source code
  • Page/URL: Identify the route and its elements
  • Feature name: Map to relevant app areas
2. Explore the Codebase

Use the Explore subagent to gather context:

  • Read playwright.config.ts for testDir, baseURL, projects
  • Check existing tests in testDir for patterns, fixtures, and conventions
  • If a component path is given, read the component to understand its props, states, and interactions
  • Check for existing page objects in pages/
  • Check for existing fixtures in fixtures/
  • Check for auth setup (auth.setup.ts or storageState config)
3. Select Templates

Check templates/ in this plugin for matching patterns:

If testing... Load template from
Login/auth flow ../pw/templates/auth/login.md
CRUD operations templates/crud/
Checkout/payment templates/checkout/
Search/filter UI templates/search/
Form submission templates/forms/
Dashboard/data templates/dashboard/
Settings page templates/settings/
Onboarding flow templates/onboarding/
API endpoints templates/api/
Accessibility templates/accessibility/

Adapt the template to the specific app — replace {{placeholders}} with actual selectors, URLs, and data.

4. Generate the Test

Follow these rules:

Structure:

import { test, expect } from '@playwright/test';
// Import custom fixtures if the project uses them

test.describe('Feature Name', () => {
  // Group related behaviors

  test('should <expected behavior>', async ({ page }) => {
    // Arrange: navigate, set up state
    // Act: perform user action
    // Assert: verify outcome
  });
});

Locator priority (use the first that works):

  1. getByRole() — buttons, links, headings, form elements
  2. getByLabel() — form fields with labels
  3. getByText() — non-interactive text content
  4. getByPlaceholder() — inputs with placeholder text
  5. getByTestId() — when semantic options aren't available

Assertions — always web-first:

// GOOD — auto-retries
await expect(page.getByRole('heading')).toBeVisible();
await expect(page.getByRole('alert')).toHaveText('Success');

// BAD — no retry
const text = await page.textContent('.msg');
expect(text).toBe('Success');

Never use:

  • page.waitForTimeout()
  • page.$(selector) or page.$$(selector)
  • Bare CSS selectors unless absolutely necessary
  • page.evaluate() for things locators can do

Always include:

  • Descriptive test names that explain the behavior
  • Error/edge case tests alongside happy path
  • Proper await on every Playwright call
  • baseURL-relative navigation (page.goto('/') not page.goto('http://...'))
5. Match Project Conventions
  • If project uses TypeScript → generate .spec.ts
  • If project uses JavaScript → generate .spec.js with require() imports
  • If project has page objects → use them instead of inline locators
  • If project has custom fixtures → import and use them
  • If project has a test data directory → create test data files there
6. Generate Supporting Files (If Needed)
  • Page object: If the test touches 5+ unique locators on one page, create a page object
  • Fixture: If the test needs shared setup (auth, data), create or extend a fixture
  • Test data: If the test uses structured data, create a JSON file in test-data/
7. Verify

Run the generated test:

npx playwright test <generated-file> --reporter=list

If it fails:

  1. Read the error
  2. Fix the test (not the app)
  3. Run again
  4. If it's an app issue, report it to the user

Output

  • Generated test file(s) with path
  • Any supporting files created (page objects, fixtures, data)
  • Test run result
  • Coverage note: what behaviors are now tested
1---
2name: "generate"
3description: >-
4 Generate Playwright tests. Use when user says "write tests", "generate tests",
5 "add tests for", "test this component", "e2e test", "create test for",
6 "test this page", or "test this feature".
7---
8 
9# Generate Playwright Tests
10 
11Generate production-ready Playwright tests from a user story, URL, component name, or feature description.
12 
13## Input
14 
15`$ARGUMENTS` contains what to test. Examples:
16- `"user can log in with email and password"`
17- `"the checkout flow"`
18- `"src/components/UserProfile.tsx"`
19- `"the search page with filters"`
20 
21## Steps
22 
23### 1. Understand the Target
24 
25Parse `$ARGUMENTS` to determine:
26- **User story**: Extract the behavior to verify
27- **Component path**: Read the component source code
28- **Page/URL**: Identify the route and its elements
29- **Feature name**: Map to relevant app areas
30 
31### 2. Explore the Codebase
32 
33Use the `Explore` subagent to gather context:
34 
35- Read `playwright.config.ts` for `testDir`, `baseURL`, `projects`
36- Check existing tests in `testDir` for patterns, fixtures, and conventions
37- If a component path is given, read the component to understand its props, states, and interactions
38- Check for existing page objects in `pages/`
39- Check for existing fixtures in `fixtures/`
40- Check for auth setup (`auth.setup.ts` or `storageState` config)
41 
42### 3. Select Templates
43 
44Check `templates/` in this plugin for matching patterns:
45 
46| If testing... | Load template from |
47|---|---|
48| Login/auth flow | `../pw/templates/auth/login.md` |
49| CRUD operations | `templates/crud/` |
50| Checkout/payment | `templates/checkout/` |
51| Search/filter UI | `templates/search/` |
52| Form submission | `templates/forms/` |
53| Dashboard/data | `templates/dashboard/` |
54| Settings page | `templates/settings/` |
55| Onboarding flow | `templates/onboarding/` |
56| API endpoints | `templates/api/` |
57| Accessibility | `templates/accessibility/` |
58 
59Adapt the template to the specific app — replace `{{placeholders}}` with actual selectors, URLs, and data.
60 
61### 4. Generate the Test
62 
63Follow these rules:
64 
65**Structure:**
66```typescript
67import { test, expect } from '@playwright/test';
68// Import custom fixtures if the project uses them
69 
70test.describe('Feature Name', () => {
71 // Group related behaviors
72 
73 test('should <expected behavior>', async ({ page }) => {
74 // Arrange: navigate, set up state
75 // Act: perform user action
76 // Assert: verify outcome
77 });
78});
79```
80 
81**Locator priority** (use the first that works):
821. `getByRole()` — buttons, links, headings, form elements
832. `getByLabel()` — form fields with labels
843. `getByText()` — non-interactive text content
854. `getByPlaceholder()` — inputs with placeholder text
865. `getByTestId()` — when semantic options aren't available
87 
88**Assertions** — always web-first:
89```typescript
90// GOOD — auto-retries
91await expect(page.getByRole('heading')).toBeVisible();
92await expect(page.getByRole('alert')).toHaveText('Success');
93 
94// BAD — no retry
95const text = await page.textContent('.msg');
96expect(text).toBe('Success');
97```
98 
99**Never use:**
100- `page.waitForTimeout()`
101- `page.$(selector)` or `page.$$(selector)`
102- Bare CSS selectors unless absolutely necessary
103- `page.evaluate()` for things locators can do
104 
105**Always include:**
106- Descriptive test names that explain the behavior
107- Error/edge case tests alongside happy path
108- Proper `await` on every Playwright call
109- `baseURL`-relative navigation (`page.goto('/')` not `page.goto('http://...')`)
110 
111### 5. Match Project Conventions
112 
113- If project uses TypeScript → generate `.spec.ts`
114- If project uses JavaScript → generate `.spec.js` with `require()` imports
115- If project has page objects → use them instead of inline locators
116- If project has custom fixtures → import and use them
117- If project has a test data directory → create test data files there
118 
119### 6. Generate Supporting Files (If Needed)
120 
121- **Page object**: If the test touches 5+ unique locators on one page, create a page object
122- **Fixture**: If the test needs shared setup (auth, data), create or extend a fixture
123- **Test data**: If the test uses structured data, create a JSON file in `test-data/`
124 
125### 7. Verify
126 
127Run the generated test:
128 
129```bash
130npx playwright test <generated-file> --reporter=list
131```
132 
133If it fails:
1341. Read the error
1352. Fix the test (not the app)
1363. Run again
1374. If it's an app issue, report it to the user
138 
139## Output
140 
141- Generated test file(s) with path
142- Any supporting files created (page objects, fixtures, data)
143- Test run result
144- Coverage note: what behaviors are now tested
145 

Discussion

Alternatives

Also in Specs & PRDsSee all 277 in Product →