playwright-best-practices

Defines Playwright E2E testing standards for Python pytest projects including locators, assertions, and POM patterns.

1|Updated Jul 12, 2026
One-click install
npx skills add https://github.com/marcocelone/demo-python-ai-agent-project --skill playwright-best-practices-marcocelone
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: playwright-best-practices
Source: https://github.com/marcocelone/demo-python-ai-agent-project/tree/main/.claude/skills/playwright-best-practices
Command: npx skills add https://github.com/marcocelone/demo-python-ai-agent-project --skill playwright-best-practices-marcocelone

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing Playwright E2E tests without shared standards leads to flaky selectors, arbitrary sleeps, and inconsistent test structure across a team. This Skill provides a single authoritative reference of locator priorities, assertion patterns, wait strategies, and anti-patterns so every test follows the same conventions. ## Core Features & Use Cases - Locator Strategy: Enforces a priority order from data-testid down to CSS classes, banning XPath and fragile CSS chains. - Test Structure & POM: Defines Page Object Model rules, fixture patterns like clean_session, and file naming conventions for pytest. - Wait & Assertion Patterns: Mandates auto-waiting expect() assertions and forbids time.sleep and wait_for_timeout. - Use Case: When asked to write a new booking flow test for the EventHub app, the agent follows these standards to produce a test using get_by_test_id locators, a POM action method, and expect-based assertions. ## Quick Start Write a Playwright pytest test for the booking cancellation flow following the project best practices.

Frequently Asked Questions about playwright-best-practices

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write Playwright tests in Python with pytest?▼

Use pytest-playwright with the page fixture, organize tests in tests/test_<feature>.py files, and follow a setup-action-assert structure. Store locators in Page Object Model classes under pages/ and keep assertions in test files using expect().

What locator strategy should I use in Playwright?▼

Prefer get_by_test_id first, then get_by_role, get_by_label, and get_by_placeholder, with element IDs and CSS classes as last resorts. Avoid XPath selectors, complex CSS chains, and index-based selectors because they are fragile.

How do I avoid flaky waits in Playwright tests?▼

Use expect().to_be_visible() which auto-waits, or locator.wait_for() inside page objects. Never use time.sleep() or page.wait_for_timeout(), since fixed delays cause flakiness and slow down the suite.

Should assertions go inside Page Object Model methods?▼

No, assertions belong in test files, not in page objects. POM classes should only store locators in __init__ and expose user-action methods, keeping verification logic in the tests themselves.

How do I mock API responses in Playwright Python tests?▼

Use page.route() with a URL pattern like "**/api/events**" and a handler that calls route.fulfill() with a status, content type, and JSON body. This intercepts network calls before they reach the server.