Comprehensive testing optimization guide for Playwright with Next.js applications. Contains 43 rules across 8 categories, prioritized by impact to guide reliable, fast, and maintainable E2E tests.
When to Apply
Reference these guidelines when:
Writing new Playwright tests for Next.js apps
Debugging flaky or failing tests
Optimizing test execution speed
Setting up authentication state reuse
Configuring CI/CD pipelines for testing
Testing Server Components and App Router features
Reviewing test code for reliability issues
Rule Categories by Priority
Priority
Category
Impact
Prefix
1
Test Architecture
CRITICAL
arch-
2
Selectors & Locators
CRITICAL
loc-
3
Waiting & Assertions
HIGH
wait-
4
Authentication & State
HIGH
auth-
5
Mocking & Network
MEDIUM-HIGH
mock-
6
Next.js Integration
MEDIUM
next-
7
Performance & Speed
MEDIUM
perf-
8
Debugging & CI
LOW-MEDIUM
debug-
Quick Reference
1. Test Architecture (CRITICAL)
arch-test-isolation - Use fresh browser context for each test
arch-parallel-execution - Enable parallel test execution
arch-page-object-model - Use Page Object Model for complex pages
arch-fixtures - Use fixtures for shared setup
arch-test-production - Test against production builds
arch-cleanup-state - Clean up test state after each test
2. Selectors & Locators (CRITICAL)
loc-role-selectors - Use role-based selectors over CSS
loc-data-testid - Use data-testid for dynamic elements
loc-label-selectors - Use getByLabel for form inputs
loc-text-selectors - Use getByText for static content
loc-avoid-xpath - Avoid XPath selectors
loc-chained-locators - Chain locators for specificity
loc-placeholder-selector - Use getByPlaceholder sparingly
3. Waiting & Assertions (HIGH)
wait-web-first-assertions - Use web-first assertions
wait-avoid-hard-waits - Avoid hard waits
wait-network-idle - Use network idle for complex pages
wait-action-retries - Let actions auto-wait before interacting
wait-soft-assertions - Use soft assertions for non-critical checks
perf-reuse-server - Reuse development server when possible
perf-retries - Configure retries for flaky test recovery
8. Debugging & CI (LOW-MEDIUM)
debug-trace-viewer - Use trace viewer for failed tests
debug-screenshots-videos - Capture screenshots and videos on failure
debug-inspector - Use Playwright Inspector for interactive debugging
debug-ci-reporters - Configure reporters for CI integration
How to Use
Read individual reference files for detailed explanations and code examples:
Section definitions - Category structure and impact levels
Rule template - Template for adding new rules
Reference Files
File
Description
AGENTS.md
Complete compiled guide with all rules
references/_sections.md
Category definitions and ordering
assets/templates/_template.md
Template for new rules
metadata.json
Version and reference information
1---2name: playwright3description: Playwright + Next.js Testing Best Practices4---5# Playwright + Next.js Testing Best Practices67Comprehensive testing optimization guide for Playwright with Next.js applications. Contains 43 rules across 8 categories, prioritized by impact to guide reliable, fast, and maintainable E2E tests.89## When to Apply1011Reference these guidelines when:12- Writing new Playwright tests for Next.js apps13- Debugging flaky or failing tests14- Optimizing test execution speed15- Setting up authentication state reuse16- Configuring CI/CD pipelines for testing17- Testing Server Components and App Router features18- Reviewing test code for reliability issues1920## Rule Categories by Priority2122| Priority | Category | Impact | Prefix |23|----------|----------|--------|--------|24| 1 | Test Architecture | CRITICAL | `arch-` |25| 2 | Selectors & Locators | CRITICAL | `loc-` |26| 3 | Waiting & Assertions | HIGH | `wait-` |27| 4 | Authentication & State | HIGH | `auth-` |28| 5 | Mocking & Network | MEDIUM-HIGH | `mock-` |29| 6 | Next.js Integration | MEDIUM | `next-` |30| 7 | Performance & Speed | MEDIUM | `perf-` |31| 8 | Debugging & CI | LOW-MEDIUM | `debug-` |3233## Quick Reference3435### 1. Test Architecture (CRITICAL)3637- [`arch-test-isolation`](references/arch-test-isolation.md) - Use fresh browser context for each test38- [`arch-parallel-execution`](references/arch-parallel-execution.md) - Enable parallel test execution39- [`arch-page-object-model`](references/arch-page-object-model.md) - Use Page Object Model for complex pages40- [`arch-fixtures`](references/arch-fixtures.md) - Use fixtures for shared setup41- [`arch-test-production`](references/arch-test-production.md) - Test against production builds42- [`arch-cleanup-state`](references/arch-cleanup-state.md) - Clean up test state after each test4344### 2. Selectors & Locators (CRITICAL)4546- [`loc-role-selectors`](references/loc-role-selectors.md) - Use role-based selectors over CSS47- [`loc-data-testid`](references/loc-data-testid.md) - Use data-testid for dynamic elements48- [`loc-label-selectors`](references/loc-label-selectors.md) - Use getByLabel for form inputs49- [`loc-text-selectors`](references/loc-text-selectors.md) - Use getByText for static content50- [`loc-avoid-xpath`](references/loc-avoid-xpath.md) - Avoid XPath selectors51- [`loc-chained-locators`](references/loc-chained-locators.md) - Chain locators for specificity52- [`loc-placeholder-selector`](references/loc-placeholder-selector.md) - Use getByPlaceholder sparingly5354### 3. Waiting & Assertions (HIGH)5556- [`wait-web-first-assertions`](references/wait-web-first-assertions.md) - Use web-first assertions57- [`wait-avoid-hard-waits`](references/wait-avoid-hard-waits.md) - Avoid hard waits58- [`wait-network-idle`](references/wait-network-idle.md) - Use network idle for complex pages59- [`wait-action-retries`](references/wait-action-retries.md) - Let actions auto-wait before interacting60- [`wait-soft-assertions`](references/wait-soft-assertions.md) - Use soft assertions for non-critical checks61- [`wait-custom-timeout`](references/wait-custom-timeout.md) - Configure timeouts appropriately6263### 4. Authentication & State (HIGH)6465- [`auth-storage-state`](references/auth-storage-state.md) - Reuse authentication with storage state66- [`auth-multiple-roles`](references/auth-multiple-roles.md) - Use separate storage states for different roles67- [`auth-session-storage`](references/auth-session-storage.md) - Handle session storage for auth68- [`auth-api-login`](references/auth-api-login.md) - Use API login for faster auth setup69- [`auth-parallel-workers`](references/auth-parallel-workers.md) - Use worker-scoped auth for parallel tests7071### 5. Mocking & Network (MEDIUM-HIGH)7273- [`mock-api-responses`](references/mock-api-responses.md) - Mock API responses for deterministic tests74- [`mock-intercept-modify`](references/mock-intercept-modify.md) - Intercept and modify real responses75- [`mock-har-files`](references/mock-har-files.md) - Use HAR files for complex mock scenarios76- [`mock-abort-requests`](references/mock-abort-requests.md) - Abort unnecessary requests77- [`mock-network-conditions`](references/mock-network-conditions.md) - Simulate network conditions7879### 6. Next.js Integration (MEDIUM)8081- [`next-wait-hydration`](references/next-wait-hydration.md) - Wait for hydration before interacting82- [`next-server-components`](references/next-server-components.md) - Test server components correctly83- [`next-app-router-navigation`](references/next-app-router-navigation.md) - Test App Router navigation patterns84- [`next-server-actions`](references/next-server-actions.md) - Test server actions end-to-end85- [`next-baseurl-config`](references/next-baseurl-config.md) - Configure baseURL for clean navigation8687### 7. Performance & Speed (MEDIUM)8889- [`perf-sharding`](references/perf-sharding.md) - Use sharding for large test suites90- [`perf-headless-ci`](references/perf-headless-ci.md) - Use headless mode in CI91- [`perf-browser-selection`](references/perf-browser-selection.md) - Select browsers strategically92- [`perf-reuse-server`](references/perf-reuse-server.md) - Reuse development server when possible93- [`perf-retries`](references/perf-retries.md) - Configure retries for flaky test recovery9495### 8. Debugging & CI (LOW-MEDIUM)9697- [`debug-trace-viewer`](references/debug-trace-viewer.md) - Use trace viewer for failed tests98- [`debug-screenshots-videos`](references/debug-screenshots-videos.md) - Capture screenshots and videos on failure99- [`debug-inspector`](references/debug-inspector.md) - Use Playwright Inspector for interactive debugging100- [`debug-ci-reporters`](references/debug-ci-reporters.md) - Configure reporters for CI integration101102## How to Use103104Read individual reference files for detailed explanations and code examples:105106- [Section definitions](references/_sections.md) - Category structure and impact levels107- [Rule template](assets/templates/_template.md) - Template for adding new rules108109## Reference Files110111| File | Description |112|------|-------------|113| [AGENTS.md](AGENTS.md) | Complete compiled guide with all rules |114| [references/_sections.md](references/_sections.md) | Category definitions and ordering |115| [assets/templates/_template.md](assets/templates/_template.md) | Template for new rules |116| [metadata.json](metadata.json) | Version and reference information |
Run npx skillmds@latest add comeonoliver/playwright in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
Playwright + Next.js Testing Best Practices It is listed under Web & Frontend on SkillMD.
This skill has not completed SkillMD's automated safety review yet. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
ComeOnOliver (@comeonoliver) published this skill. Their other Agent Skills are listed on their SkillMD profile.