e2e-testing
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseE2E Testing
E2E测试
Overview
概述
Covers E2E test architecture and patterns using Playwright — how to structure test suites, organize tests, and apply proven patterns for authentication, mocking, visual regression, accessibility auditing, and CI parallelism. Focuses on the WHY and WHEN of test patterns rather than Playwright API specifics.
When to use: Designing test suite architecture, structuring Page Object Models, planning CI sharding strategies, setting up authentication flows, organizing tests with tags and annotations, implementing visual regression workflows, mocking network requests, auditing accessibility.
When NOT to use: Unit testing (use Vitest/Jest), API-only testing (use integration tests), component testing in isolation (use component test runners). For Playwright API details, browser automation, scraping, or troubleshooting Playwright errors, use the skill.
playwright本文介绍基于Playwright的E2E测试架构与模式——包括如何构建测试套件、组织测试,以及在认证、模拟、视觉回归、可访问性审计和CI并行执行等场景下应用经过验证的模式。重点讲解测试模式的应用原因与适用场景,而非Playwright API的具体细节。
适用场景: 设计测试套件架构、构建Page Object Model、规划CI分片策略、设置认证流程、使用标签和注解组织测试、实现视觉回归工作流、模拟网络请求、可访问性审计。
不适用场景: 单元测试(请使用Vitest/Jest)、仅API测试(请使用集成测试)、独立组件测试(请使用组件测试运行器)。若需了解Playwright API细节、浏览器自动化、网页爬取或排查Playwright错误,请使用技能。
playwrightQuick Reference
快速参考
| Pattern | API/Tool | Key Points |
|---|---|---|
| Role-based locators | | Preferred over CSS/XPath selectors |
| Page Object Model | Classes in | Encapsulate all page-specific locators and actions |
| Authentication | | Authenticate once, reuse across tests |
| Visual regression | | Mask dynamic content to prevent flakes |
| Accessibility audit | | |
| Trace debugging | | Full DOM snapshots, network logs, and timeline |
| Network mocking | | Stable tests without third-party dependencies |
| HAR replay | | High-fidelity mocks from recorded traffic |
| Image blocking | | Speed up tests by skipping images |
| CI sharding | | Split suite across parallel CI machines |
| Blob reports | | Merge sharded results into a single report |
| Test tags | | Filter tests by category with |
| Test steps | | Group actions in trace viewer and reports |
| Changed tests only | | Run only test files changed since base branch |
| Native a11y checks | | Lightweight alternative to full axe-core scans |
| Git info in reports | | Link test reports to commits for CI debugging |
| Web-first assertions | | Auto-wait instead of |
| 模式 | API/工具 | 核心要点 |
|---|---|---|
| 基于角色的定位器 | | 优先于CSS/XPath选择器使用 |
| Page Object Model | | 封装所有页面专属的定位器与操作 |
| 认证机制 | | 仅认证一次,在所有测试中复用状态 |
| 视觉回归测试 | | 屏蔽动态内容以避免不稳定测试 |
| 可访问性审计 | | 针对核心流程使用 |
| 追踪调试 | | 完整DOM快照、网络日志与时间线记录 |
| 网络模拟 | | 脱离第三方依赖,实现稳定测试 |
| HAR重放 | | 基于录制流量生成高保真模拟数据 |
| 图片拦截 | | 跳过图片加载以加速测试 |
| CI分片 | | 在多台并行CI机器上拆分测试套件 |
| Blob报告 | | 将分片测试结果合并为单个报告 |
| 测试标签 | | 使用 |
| 测试步骤 | | 在追踪查看器与报告中分组展示操作 |
| 仅运行变更测试 | | 仅运行自基准分支以来修改过的测试文件 |
| 原生可访问性检查 | | 轻量级替代方案,无需完整axe-core扫描 |
| 报告中嵌入Git信息 | | 将测试报告与提交记录关联,便于CI调试 |
| 优先Web断言 | | 使用自动等待替代 |
Common Mistakes
常见误区
| Mistake | Correct Pattern |
|---|---|
Using | Use web-first assertions like |
| Writing raw locators directly in test files | Encapsulate all selectors in Page Object Model classes |
| Testing third-party APIs (Stripe, Auth0) directly | Mock external services with |
| Debugging CI failures with screenshots instead of traces | Configure |
| Sharing state between tests via global variables | Use a fresh |
| Running all tests in a single CI job | Use Playwright sharding ( |
| Using CSS/XPath selectors for element location | Use role-based locators that survive refactors and enforce accessibility |
| Logging in via UI in every test | Use |
Using | Use |
Committing | Add |
| 错误做法 | 正确解决方案 |
|---|---|
使用 | 使用 |
| 在测试文件中直接编写原生定位器 | 将所有选择器封装到Page Object Model类中 |
| 直接测试第三方API(如Stripe、Auth0) | 使用 |
| 仅通过截图调试CI失败问题 | 配置 |
| 通过全局变量在测试间共享状态 | 为每个测试使用全新的 |
| 在单个CI任务中运行所有测试 | 使用Playwright分片功能( |
| 使用CSS/XPath选择器定位元素 | 使用基于角色的定位器,可在重构后仍保持有效且符合可访问性要求 |
| 在每个测试中通过UI重新登录 | 结合 |
使用 | 使用 |
将 | 将 |
Delegation
任务委托
- Discover which user flows lack E2E coverage: Use agent
Explore - Build a full Page Object Model test suite for an application: Use agent
Task - Plan a CI sharding strategy for a large test suite: Use agent
Plan
For Playwright API details, browser automation, scraping, stealth mode, screenshots/PDFs, Docker deployment, or troubleshooting Playwright errors, use theskill.playwright
- 发现缺少E2E覆盖的用户流程:使用agent
Explore - 为应用构建完整的Page Object Model测试套件:使用agent
Task - 为大型测试套件规划CI分片策略:使用agent
Plan
若需了解Playwright API细节、浏览器自动化、网页爬取、隐身模式、截图/PDF生成、Docker部署或排查Playwright错误,请使用技能。playwright
References
参考资料
- Role-based locators, auto-waiting, chaining, filtering, and locator strategy
- Page Object Model architecture, fixtures integration, and base page patterns
- Authentication with storageState, setup projects, and multi-role testing
- Network mocking, route interception, HAR replay, and WebSocket mocking
- Accessibility auditing with AxeBuilder, WCAG tags, and reusable fixtures
- Visual regression, snapshot testing, masking, tolerance thresholds, and baseline management
- CI sharding, parallelism, blob reports, and browser caching
- Test organization with tags, annotations, test.step, and project configuration
- 基于角色的定位器、自动等待、链式调用、筛选与定位策略
- Page Object Model架构、fixtures集成与基础页面模式
- 基于storageState的认证、初始化项目与多角色测试
- 网络模拟、路由拦截、HAR重放与WebSocket模拟
- 基于AxeBuilder的可访问性审计、WCAG标签与可复用fixtures
- 视觉回归、快照测试、内容屏蔽、容差阈值与基线管理
- CI分片、并行执行、Blob报告与浏览器缓存
- 基于标签、注解、test.step与项目配置的测试组织