playwright
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePlaywright + Next.js Testing Best Practices
Playwright + Next.js 测试最佳实践
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.
针对Next.js应用的Playwright全面测试优化指南。包含8个类别下的43条规则,按影响程度排序,指导编写可靠、快速且可维护的E2E测试。
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
在以下场景中参考这些指南:
- 为Next.js应用编写新的Playwright测试
- 调试不稳定或失败的测试
- 优化测试执行速度
- 设置认证状态复用
- 配置用于测试的CI/CD流水线
- 测试Server Components和App Router功能
- 评审测试代码以排查可靠性问题
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Test Architecture | CRITICAL | |
| 2 | Selectors & Locators | CRITICAL | |
| 3 | Waiting & Assertions | HIGH | |
| 4 | Authentication & State | HIGH | |
| 5 | Mocking & Network | MEDIUM-HIGH | |
| 6 | Next.js Integration | MEDIUM | |
| 7 | Performance & Speed | MEDIUM | |
| 8 | Debugging & CI | LOW-MEDIUM | |
| 优先级 | 类别 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | 测试架构 | 关键 | |
| 2 | 选择器与定位器 | 关键 | |
| 3 | 等待与断言 | 高 | |
| 4 | 认证与状态 | 高 | |
| 5 | 模拟与网络 | 中高 | |
| 6 | Next.js集成 | 中 | |
| 7 | 性能与速度 | 中 | |
| 8 | 调试与CI | 中低 | |
Quick Reference
快速参考
1. Test Architecture (CRITICAL)
1. 测试架构(关键)
- - Use fresh browser context for each test
arch-test-isolation - - Enable parallel test execution
arch-parallel-execution - - Use Page Object Model for complex pages
arch-page-object-model - - Use fixtures for shared setup
arch-fixtures - - Test against production builds
arch-test-production - - Clean up test state after each test
arch-cleanup-state
- - 为每个测试使用全新的浏览器上下文
arch-test-isolation - - 启用测试并行执行
arch-parallel-execution - - 为复杂页面使用Page Object Model
arch-page-object-model - - 使用fixtures进行共享设置
arch-fixtures - - 针对生产构建进行测试
arch-test-production - - 每个测试后清理测试状态
arch-cleanup-state
2. Selectors & Locators (CRITICAL)
2. 选择器与定位器(关键)
- - Use role-based selectors over CSS
loc-role-selectors - - Use data-testid for dynamic elements
loc-data-testid - - Use getByLabel for form inputs
loc-label-selectors - - Use getByText for static content
loc-text-selectors - - Avoid XPath selectors
loc-avoid-xpath - - Chain locators for specificity
loc-chained-locators - - Use getByPlaceholder sparingly
loc-placeholder-selector
- - 优先使用基于角色的选择器而非CSS选择器
loc-role-selectors - - 为动态元素使用data-testid
loc-data-testid - - 为表单输入使用getByLabel
loc-label-selectors - - 为静态内容使用getByText
loc-text-selectors - - 避免使用XPath选择器
loc-avoid-xpath - - 链式使用定位器以提高特异性
loc-chained-locators - - 谨慎使用getByPlaceholder
loc-placeholder-selector
3. Waiting & Assertions (HIGH)
3. 等待与断言(高)
- - Use web-first assertions
wait-web-first-assertions - - Avoid hard waits
wait-avoid-hard-waits - - Use network idle for complex pages
wait-network-idle - - Let actions auto-wait before interacting
wait-action-retries - - Use soft assertions for non-critical checks
wait-soft-assertions - - Configure timeouts appropriately
wait-custom-timeout
- - 使用web-first断言
wait-web-first-assertions - - 避免硬等待
wait-avoid-hard-waits - - 为复杂页面使用网络空闲状态
wait-network-idle - - 让操作在交互前自动等待
wait-action-retries - - 为非关键检查使用软断言
wait-soft-assertions - - 合理配置超时时间
wait-custom-timeout
4. Authentication & State (HIGH)
4. 认证与状态(高)
- - Reuse authentication with storage state
auth-storage-state - - Use separate storage states for different roles
auth-multiple-roles - - Handle session storage for auth
auth-session-storage - - Use API login for faster auth setup
auth-api-login - - Use worker-scoped auth for parallel tests
auth-parallel-workers
- - 使用存储状态复用认证信息
auth-storage-state - - 为不同角色使用独立的存储状态
auth-multiple-roles - - 处理认证相关的会话存储
auth-session-storage - - 使用API登录以加快认证设置
auth-api-login - - 为并行测试使用工作进程级别的认证
auth-parallel-workers
5. Mocking & Network (MEDIUM-HIGH)
5. 模拟与网络(中高)
- - Mock API responses for deterministic tests
mock-api-responses - - Intercept and modify real responses
mock-intercept-modify - - Use HAR files for complex mock scenarios
mock-har-files - - Abort unnecessary requests
mock-abort-requests - - Simulate network conditions
mock-network-conditions
- - 模拟API响应以实现确定性测试
mock-api-responses - - 拦截并修改真实响应
mock-intercept-modify - - 为复杂模拟场景使用HAR文件
mock-har-files - - 中止不必要的请求
mock-abort-requests - - 模拟网络条件
mock-network-conditions
6. Next.js Integration (MEDIUM)
6. Next.js集成(中)
- - Wait for hydration before interacting
next-wait-hydration - - Test server components correctly
next-server-components - - Test App Router navigation patterns
next-app-router-navigation - - Test server actions end-to-end
next-server-actions - - Configure baseURL for clean navigation
next-baseurl-config
- - 交互前等待水合完成
next-wait-hydration - - 正确测试Server Components
next-server-components - - 测试App Router导航模式
next-app-router-navigation - - 端到端测试Server Actions
next-server-actions - - 配置baseURL以实现清晰导航
next-baseurl-config
7. Performance & Speed (MEDIUM)
7. 性能与速度(中)
- - Use sharding for large test suites
perf-sharding - - Use headless mode in CI
perf-headless-ci - - Select browsers strategically
perf-browser-selection - - Reuse development server when possible
perf-reuse-server - - Configure retries for flaky test recovery
perf-retries
- - 为大型测试套件使用分片
perf-sharding - - 在CI中使用无头模式
perf-headless-ci - - 策略性选择浏览器
perf-browser-selection - - 尽可能复用开发服务器
perf-reuse-server - - 配置重试以恢复不稳定测试
perf-retries
8. Debugging & CI (LOW-MEDIUM)
8. 调试与CI(中低)
- - Use trace viewer for failed tests
debug-trace-viewer - - Capture screenshots and videos on failure
debug-screenshots-videos - - Use Playwright Inspector for interactive debugging
debug-inspector - - Configure reporters for CI integration
debug-ci-reporters
- - 为失败测试使用跟踪查看器
debug-trace-viewer - - 测试失败时捕获截图和视频
debug-screenshots-videos - - 使用Playwright Inspector进行交互式调试
debug-inspector - - 配置报告器以集成CI
debug-ci-reporters
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
阅读单个参考文件以获取详细说明和代码示例:
- Section definitions - 类别结构和影响级别
- Rule template - 添加新规则的模板
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 |
| 文件 | 描述 |
|---|---|
| AGENTS.md | 包含所有规则的完整编译指南 |
| references/_sections.md | 类别定义和排序 |
| assets/templates/_template.md | 新规则模板 |
| metadata.json | 版本和参考信息 |