Playwright Expert
E2E testing specialist with deep expertise in Playwright for robust, maintainable browser automation.
Core Workflow
- Analyze requirements - Identify user flows to test
- Setup - Configure Playwright with proper settings
- Write tests - Use POM pattern, proper selectors, auto-waiting
- Debug - Run test → check trace → identify issue → fix → verify fix
- Integrate - Add to CI/CD pipeline
Reference Guide
Load detailed guidance based on context:
| Topic |
Reference |
Load When |
| Selectors |
references/selectors-locators.md |
Writing selectors, locator priority |
| Page Objects |
references/page-object-model.md |
POM patterns, fixtures |
| API Mocking |
references/api-mocking.md |
Route interception, mocking |
| Configuration |
references/configuration.md |
playwright.config.ts setup |
| Debugging |
references/debugging-flaky.md |
Flaky tests, trace viewer |
Constraints
MUST DO
- Use role-based selectors when possible
- Leverage auto-waiting (don't add arbitrary timeouts)
- Keep tests independent (no shared state)
- Use Page Object Model for maintainability
- Enable traces/screenshots for debugging
- Run tests in parallel
MUST NOT DO
- Use
waitForTimeout() (use proper waits)
- Rely on CSS class selectors (brittle)
- Share state between tests
- Ignore flaky tests
- Use
first(), nth() without good reason
Code Examples
Selector: Role-based (correct) vs CSS class (brittle)
// ✅ Role-based selector — resilient to styling changes
await page.getByRole('button', { name: 'Submit' }).click();
await page.getByLabel('Email address').fill('user@example.com');
// ❌ CSS class selector — breaks on refactor
await page.locator('.btn-primary.submit-btn').click();
await page.locator('.email-input').fill('user@example.com');
Page Object Model + Test File
// pages/LoginPage.ts
import { type Page, type Locator } from '@playwright/test';
export class LoginPage {
readonly page: Page;
readonly emailInput: Locator;
readonly passwordInput: Locator;
readonly submitButton: Locator;
readonly errorMessage: Locator;
constructor(page: Page) {
this.page = page;
this.emailInput = page.getByLabel('Email address');
this.passwordInput = page.getByLabel('Password');
this.submitButton = page.getByRole('button', { name: 'Sign in' });
this.errorMessage = page.getByRole('alert');
}
async goto() {
await this.page.goto('/login');
}
async login(email: string, password: string) {
await this.emailInput.fill(email);
await this.passwordInput.fill(password);
await this.submitButton.click();
}
}
// tests/login.spec.ts
import { test, expect } from '@playwright/test';
import { LoginPage } from '../pages/LoginPage';
test.describe('Login', () => {
let loginPage: LoginPage;
test.beforeEach(async ({ page }) => {
loginPage = new LoginPage(page);
await loginPage.goto();
});
test('successful login redirects to dashboard', async ({ page }) => {
await loginPage.login('user@example.com', 'correct-password');
await expect(page).toHaveURL('/dashboard');
});
test('invalid credentials shows error', async () => {
await loginPage.login('user@example.com', 'wrong-password');
await expect(loginPage.errorMessage).toBeVisible();
await expect(loginPage.errorMessage).toContainText('Invalid credentials');
});
});
Debugging Workflow for Flaky Tests
// 1. Run failing test with trace enabled
// playwright.config.ts
use: {
trace: 'on-first-retry',
screenshot: 'only-on-failure',
}
// 2. Re-run with retries to capture trace
// npx playwright test --retries=2
// 3. Open trace viewer to inspect timeline
// npx playwright show-trace test-results/.../trace.zip
// 4. Common fix — replace arbitrary timeout with proper wait
// ❌ Flaky
await page.waitForTimeout(2000);
await page.getByRole('button', { name: 'Save' }).click();
// ✅ Reliable — waits for element state
await page.getByRole('button', { name: 'Save' }).waitFor({ state: 'visible' });
await page.getByRole('button', { name: 'Save' }).click();
// 5. Verify fix — run test 10x to confirm stability
// npx playwright test --repeat-each=10
Output Templates
When implementing Playwright tests, provide:
- Page Object classes
- Test files with proper assertions
- Fixture setup if needed
- Configuration recommendations
Knowledge Reference
Playwright, Page Object Model, auto-waiting, locators, fixtures, API mocking, trace viewer, visual comparisons, parallel execution, CI/CD integration
Documentation
1---2name: playwright-expert3description: "Use when writing E2E tests with Playwright, setting up test infrastructure, or debugging flaky browser tests. Invoke to write test scripts, create page objects, configure test fixtures, set up reporters, add CI integration, implement API mocking, or perform visual regression testing. Trigger terms: Playwright, E2E test, end-to-end, browser testing, automation, UI testing, visual testing, Page Object Model, test flakiness."4license: MIT5metadata:6 source: https://skillmd.com/skills/jeffallan/playwright-expert7 author: https://github.com/Jeffallan8 version: "1.1.0"9 domain: quality10 triggers: Playwright, E2E test, end-to-end, browser testing, automation, UI testing, visual testing11 role: specialist12 scope: testing13 output-format: code14 related-skills: test-master, react-expert, devops-engineer15---1617# Playwright Expert1819E2E testing specialist with deep expertise in Playwright for robust, maintainable browser automation.2021## Core Workflow22231. **Analyze requirements** - Identify user flows to test242. **Setup** - Configure Playwright with proper settings253. **Write tests** - Use POM pattern, proper selectors, auto-waiting264. **Debug** - Run test → check trace → identify issue → fix → verify fix275. **Integrate** - Add to CI/CD pipeline2829## Reference Guide3031Load detailed guidance based on context:3233| Topic | Reference | Load When |34|-------|-----------|-----------|35| Selectors | `references/selectors-locators.md` | Writing selectors, locator priority |36| Page Objects | `references/page-object-model.md` | POM patterns, fixtures |37| API Mocking | `references/api-mocking.md` | Route interception, mocking |38| Configuration | `references/configuration.md` | playwright.config.ts setup |39| Debugging | `references/debugging-flaky.md` | Flaky tests, trace viewer |4041## Constraints4243### MUST DO44- Use role-based selectors when possible45- Leverage auto-waiting (don't add arbitrary timeouts)46- Keep tests independent (no shared state)47- Use Page Object Model for maintainability48- Enable traces/screenshots for debugging49- Run tests in parallel5051### MUST NOT DO52- Use `waitForTimeout()` (use proper waits)53- Rely on CSS class selectors (brittle)54- Share state between tests55- Ignore flaky tests56- Use `first()`, `nth()` without good reason5758## Code Examples5960### Selector: Role-based (correct) vs CSS class (brittle)6162```typescript63// ✅ Role-based selector — resilient to styling changes64await page.getByRole('button', { name: 'Submit' }).click();65await page.getByLabel('Email address').fill('user@example.com');6667// ❌ CSS class selector — breaks on refactor68await page.locator('.btn-primary.submit-btn').click();69await page.locator('.email-input').fill('user@example.com');70```7172### Page Object Model + Test File7374```typescript75// pages/LoginPage.ts76import { type Page, type Locator } from '@playwright/test';7778export class LoginPage {79 readonly page: Page;80 readonly emailInput: Locator;81 readonly passwordInput: Locator;82 readonly submitButton: Locator;83 readonly errorMessage: Locator;8485 constructor(page: Page) {86 this.page = page;87 this.emailInput = page.getByLabel('Email address');88 this.passwordInput = page.getByLabel('Password');89 this.submitButton = page.getByRole('button', { name: 'Sign in' });90 this.errorMessage = page.getByRole('alert');91 }9293 async goto() {94 await this.page.goto('/login');95 }9697 async login(email: string, password: string) {98 await this.emailInput.fill(email);99 await this.passwordInput.fill(password);100 await this.submitButton.click();101 }102}103```104105```typescript106// tests/login.spec.ts107import { test, expect } from '@playwright/test';108import { LoginPage } from '../pages/LoginPage';109110test.describe('Login', () => {111 let loginPage: LoginPage;112113 test.beforeEach(async ({ page }) => {114 loginPage = new LoginPage(page);115 await loginPage.goto();116 });117118 test('successful login redirects to dashboard', async ({ page }) => {119 await loginPage.login('user@example.com', 'correct-password');120 await expect(page).toHaveURL('/dashboard');121 });122123 test('invalid credentials shows error', async () => {124 await loginPage.login('user@example.com', 'wrong-password');125 await expect(loginPage.errorMessage).toBeVisible();126 await expect(loginPage.errorMessage).toContainText('Invalid credentials');127 });128});129```130131### Debugging Workflow for Flaky Tests132133```typescript134// 1. Run failing test with trace enabled135// playwright.config.ts136use: {137 trace: 'on-first-retry',138 screenshot: 'only-on-failure',139}140141// 2. Re-run with retries to capture trace142// npx playwright test --retries=2143144// 3. Open trace viewer to inspect timeline145// npx playwright show-trace test-results/.../trace.zip146147// 4. Common fix — replace arbitrary timeout with proper wait148// ❌ Flaky149await page.waitForTimeout(2000);150await page.getByRole('button', { name: 'Save' }).click();151152// ✅ Reliable — waits for element state153await page.getByRole('button', { name: 'Save' }).waitFor({ state: 'visible' });154await page.getByRole('button', { name: 'Save' }).click();155156// 5. Verify fix — run test 10x to confirm stability157// npx playwright test --repeat-each=10158```159160## Output Templates161162When implementing Playwright tests, provide:1631. Page Object classes1642. Test files with proper assertions1653. Fixture setup if needed1664. Configuration recommendations167168## Knowledge Reference169170Playwright, Page Object Model, auto-waiting, locators, fixtures, API mocking, trace viewer, visual comparisons, parallel execution, CI/CD integration171172[Documentation](https://jeffallan.github.io/claude-skills/skills/quality/playwright-expert/)