Playwright best practices
Skill bg-szy/TOP-SKILLS/skills/marketplace/playwright-best-practices
全球最大的 Claude Code 技能聚合库 · 收录 3900+ 来自 12+ 来源的技能,提供在线搜索与趋势分析看板 / The world's largest Claude Code skill aggregation hub — 3900+ skills from 12+ sources with online search and trend dashboard
npx -y skills add bg-szy/TOP-SKILLS --skill playwright-best-practicesAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
2 things to look at
- no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.
- 4 stars4 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
What its author says it does
Copied from the file, not written here
Use when writing or modifying Playwright tests (.spec.ts, .test.ts with @playwright/test imports).
SKILL.md
2.7 KB, as published. Nobody here has run it
Playwright Best Practices
CLI Context: Prevent Context Overflow
When running Playwright tests from Claude Code or any CLI agent, always use minimal reporters to prevent verbose output from consuming the context window.
Use --reporter=line or --reporter=dot for CLI test runs. Configure playwright.config.ts to default to minimal reporters when CI or CLAUDE env vars are set — see playwright-patterns.md for the config snippet.
Locator Priority (Most to Least Resilient)
Always prefer user-facing attributes:
page.getByRole('button', { name: 'Submit' })— accessibility rolespage.getByLabel('Email')— form control labelspage.getByPlaceholder('Search...')— input placeholderspage.getByText('Welcome')— visible text (non-interactive)page.getByAltText('Logo')— image alt textpage.getByTitle('Settings')— title attributespage.getByTestId('submit-btn')— explicit test contracts- CSS/XPath — last resort, avoid
Core Rules
- Web-first assertions: always
await expect(locator).toBeVisible(), neverexpect(await locator.isVisible()).toBe(true)— web-first matchers auto-wait and retry - Test isolation: each test creates its own data; never share state between tests
- Auth state reuse: save authenticated state via setup project +
storageState; never log in via UI in every test - Fixtures over beforeEach: fixtures encapsulate setup + teardown, run on-demand, and compose
Anti-Patterns
page.waitForTimeout(ms)— use auto-waiting locators insteadpage.locator('.class')— use role/label/testid- XPath selectors — fragile, use user-facing attributes
- Shared state between tests — each test creates own data
- UI login in every test — use setup project + storageState
- Manual assertions without await — use web-first assertions
- Hardcoded waits — rely on Playwright's auto-waiting
- Default reporter in CI/agent — use
--reporter=lineor--reporter=dot
Checklist
- Locators use role/label/testid, not CSS classes or XPath
- All assertions use
await expect()web-first matchers - Page objects define locators in constructor
- No
page.waitForTimeout()— use auto-waiting - Tests isolated — no shared state
- Auth state reused via setup project
- Network mocks set up before navigation
- Test data created per-test or via fixtures
- Debug logging added for complex flows
- Minimal reporter (
line/dot) used in CI/agent contexts
See playwright-patterns.md for Page Object Model, fixtures, network mocking, and configuration examples.