Best Practices
Guidelines to keep your Playwright suite fast and reliable.
Best practices checklist
text
✅ Use semantic locators (getByRole, getByLabel) over CSS/XPath
✅ Use data-testid for stable selectors when semantic ones aren't available
✅ Run tests in parallel with fullyParallel: true
✅ Use baseURL in config — avoids hardcoded URLs in tests
✅ Mock external APIs to keep tests deterministic
✅ Use test fixtures for reusable setup (auth, page objects)
✅ Enable trace: 'on-first-retry' for CI debugging
✅ Use expect() assertions — they auto-retry
✅ Avoid page.waitForTimeout() — it's a smell
✅ Group related tests in describe blocks
✅ Use test.use({ viewport }) for responsive tests
✅ Keep test files in __tests__ or tests/ directory