--- name: playwright-best-practices description: Enforces stable, maintainable, and high-performance Playwright test code. --- # Playwright Best Practices When generating or refactoring tests, you must adhere to these standards: 1. **User-Visible Locators**: Prioritize `page.getByRole()`, `page.getByText()`, and `page.getByLabel()`. Never use fragile CSS selectors (e.g., `.btn-primary`) or XPath unless no other option exists. 2. **Web-First Assertions**: Use the `expect(locator).to...` pattern (e.g., `toBeVisible()`, `toHaveText()`) to leverage Playwright's built-in auto-waiting and retry logic. 3. **Test Isolation**: Every test must be independent. Use `test.beforeEach` for setup (like navigation or login) rather than relying on the state of a previous test. 4. **Avoid Logic in Tests**: Keep tests declarative. Use Page Object Models (POM) if the logic for finding an element requires more than one line of code. 5. **Network Reliability**: Mock third-party APIs using `page.route()` to prevent external flakiness.