Home › Testing › Playwright end-to-end testing rules

Playwright end-to-end testing rules

Cursor rules for Playwright Test (TypeScript): resilient locators, web-first assertions, fixtures, and no flaky waits.

CursorTesting

How to use

Save this as .cursor/rules/playwright.mdc in your repository root, commit it, and your agent will pick it up automatically. Edit the commands to match your project.

.cursor/rules/playwright.mdcDownload  
---
description: Playwright end-to-end testing conventions
globs: ["e2e/**/*.ts","tests/**/*.spec.ts","playwright.config.ts"]
alwaysApply: false
---

# Playwright Test

- Tests live in `e2e/` (or `tests/`) as `*.spec.ts`. Run `npx playwright test`; debug with `--ui` or `--debug`.
- Locators by user-facing role and text: `page.getByRole('button', { name: 'Save' })`, `getByLabel`, `getByText`. Use `getByTestId` only when there is no accessible name. No CSS/XPath chains tied to layout.
- Web-first assertions only: `await expect(locator).toBeVisible()`, `toHaveText`, `toHaveURL`. They retry automatically.
- Never use `page.waitForTimeout()` or fixed sleeps. Wait for a visible state, a response (`page.waitForResponse`), or a URL.
- Each test is independent: create its own data through API calls or fixtures, not by depending on another test.
- Log in once in a setup project and reuse `storageState`; don't log in through the UI in every test.
- Put repeated flows in page objects or custom fixtures (`test.extend`), not copy-pasted steps.
- Mock third-party services with `page.route`; hit your own backend for real.
- Keep `retries` low in CI (1-2) and keep `trace: 'on-first-retry'` so failures can be debugged.
- Use `test.step` to label long flows.

## Don't
- Don't use `force: true` to get past a covered element; fix the UI state.
- Don't assert on exact pixel positions or auto-generated class names.

## Done when
The new tests pass three times in a row: `npx playwright test <file> --repeat-each=3`.

More: all Testing rules · all Cursor rules

Related rules

Cypress end-to-end testing AGENTS.md

AGENTS.md for Cypress 15 E2E and component tests: selectors, cy.intercept, sessions, and flake-free waits.

AGENTS.mdTesting

Generate tests prompt file (Copilot)

A Copilot prompt file (/generate-tests) that writes focused unit tests for the selected code using the repo framework.

GitHub CopilotTesting

Jest testing rules

Cursor rules for Jest 30 test suites in TypeScript: structure, mocking boundaries, timers, and async tests.

CursorTesting

pytest testing CLAUDE.md

CLAUDE.md for Python test suites with pytest 8: fixtures, parametrize, mocking boundaries, and coverage.

Claude Code (CLAUDE.md)Testing

Test runner subagent (Claude Code)

A Claude Code subagent that runs the right tests for a change, diagnoses failures, and proposes minimal fixes.

Claude Code (CLAUDE.md)Testing

Testing .claude/rules file

A path-scoped Claude Code rules file that loads only for test files: structure, determinism, and never weakening tests.

Claude Code (CLAUDE.md)Testing

Get new rules weekly

One email a week: fresh rule files, agent tips, and what's changing in Cursor, Claude Code and Copilot. Free, unsubscribe anytime.

Opens the Rulestack Newsletter on beehiiv in a new tab. Or subscribe directly on beehiiv.