playwright
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePlaywright
Playwright
Overview
概述
Playwright is a browser automation framework for Node.js and Python supporting Chromium, Firefox, and WebKit with a single API. It provides auto-waiting, web-first assertions, and full test isolation for reliable end-to-end testing.
When to use: Browser automation, web scraping, screenshot/PDF generation, API testing, configuring Playwright Test, troubleshooting Playwright errors, stealth mode and anti-bot bypass.
When NOT to use: Simple HTTP requests (use ), unit testing (use Vitest/Jest), serverless scraping at scale (consider Cloudflare Browser Rendering). For E2E test architecture (Page Object Models, CI sharding, test organization, authentication patterns), use the skill.
fetche2e-testingPlaywright 是一款支持 Node.js 和 Python 的浏览器自动化框架,通过单一API即可操作 Chromium、Firefox 和 WebKit 浏览器。它提供自动等待、Web优先断言和完全测试隔离能力,可实现可靠的端到端测试。
适用场景: 浏览器自动化、网页抓取、截图/PDF生成、API测试、配置Playwright Test、排查Playwright错误、隐身模式与反机器人绕过。
不适用场景: 简单HTTP请求(请使用)、单元测试(请使用Vitest/Jest)、大规模无服务器抓取(可考虑Cloudflare Browser Rendering)。若涉及端到端测试架构(页面对象模型、CI分片、测试组织、认证模式),请使用技能。
fetche2e-testingQuick Reference
快速参考
| Pattern | API / Config | Key Points |
|---|---|---|
| Basic test | | Auto-wait, web-first assertions, test isolation |
| Locator | | Prefer role/label/text selectors over CSS |
| Assertion | | Auto-retrying, configurable timeout |
| API testing | | Send HTTP requests, validate responses |
| Aria snapshot | | Validate accessibility tree structure via YAML |
| Class assertion | | Match individual CSS class names (v1.52+) |
| Visible filter | | Match only visible elements (v1.51+) |
| Test step | | Timeout, skip, and attachments (v1.50+) |
| Stealth mode | | Patches 20+ detection vectors |
| Authenticated session | | Save/restore cookies and IndexedDB for persistence |
| Screenshot | | Use |
| PDF generation | | Chromium only, set |
| Docker | | Use |
| Debug methods | | No event listeners needed |
| Speedboard | HTML reporter (v1.57+) | Identifies slow tests and bottlenecks |
| Playwright Agents | | Planner, generator, healer for LLM-driven testing |
| Flaky test detection | | Exit code 1 on flaky tests in CI |
| 模式 | API / 配置 | 核心要点 |
|---|---|---|
| 基础测试 | | 自动等待、Web优先断言、测试隔离 |
| 定位器 | | 优先使用角色/标签/文本选择器而非CSS |
| 断言 | | 自动重试、可配置超时时间 |
| API测试 | | 发送HTTP请求、验证响应内容 |
| Aria快照 | | 通过YAML验证无障碍树结构 |
| 类断言 | | 匹配单个CSS类名(v1.52+) |
| 可见性筛选器 | | 仅匹配可见元素(v1.51+) |
| 测试步骤 | | 超时、跳过与附件功能(v1.50+) |
| 隐身模式 | | 修补20+种检测向量 |
| 认证会话 | | 保存/恢复Cookie与IndexedDB以实现持久化 |
| 截图 | | 先使用 |
| PDF生成 | | 仅支持Chromium,需设置 |
| Docker部署 | | 使用 |
| 调试方法 | | 无需事件监听器 |
| Speedboard | HTML报告器(v1.57+) | 识别慢测试与性能瓶颈 |
| Playwright Agents | | 基于LLM的测试规划、生成与修复工具 |
| 不稳定测试检测 | | CI中出现不稳定测试时返回退出码1 |
Common Mistakes
常见错误
| Mistake | Correct Pattern |
|---|---|
| Using CSS selectors over role selectors | Prefer |
| Not closing browser | Always |
Using | Use |
| Guard with |
| Clicking without waiting | Use |
| Shared state between tests | Each test gets fresh context via fixtures |
| Testing implementation details | Assert user-visible behavior, not DOM structure |
| Hardcoded waits for dynamic content | Wait for selector appearance or content stabilization |
Missing | All |
| Same user agent for all scraping | Rotate user agents for high-volume scraping |
| 错误操作 | 正确做法 |
|---|---|
| 使用CSS选择器而非角色选择器 | 优先使用 |
| 未关闭浏览器 | 始终在 |
使用 | 使用 |
CI代码中遗留 | 用 |
| 未等待就点击 | 使用 |
| 测试间共享状态 | 每个测试通过夹具获取全新上下文 |
| 测试实现细节 | 断言用户可见行为,而非DOM结构 |
| 为动态内容设置硬编码等待时间 | 等待选择器出现或内容稳定 |
断言时遗漏 | 所有 |
| 所有抓取使用相同用户代理 | 高容量抓取时轮换用户代理 |
Delegation
任务委托
- Selector troubleshooting: Use agent
Explore - Test pattern review: Use agent
Task - Code review: Delegate to agent
code-reviewer
For E2E test architecture, Page Object Model patterns, CI sharding strategies, authentication flows, visual regression workflows, or test organization, use theskill.e2e-testing
- 选择器排查:使用agent
Explore - 测试模式评审:使用agent
Task - 代码评审:委托给agent
code-reviewer
若涉及端到端测试架构、页面对象模型模式、CI分片策略、认证流程、视觉回归工作流或测试组织,请使用技能。e2e-testing
References
参考资料
- Quick start and installation
- E2E testing patterns and assertions
- Selector strategies and best practices
- Configuration and Docker deployment
- Docker and CI
- Debug methods and performance analysis
- Common automation patterns
- Stealth mode and anti-bot bypass
- Known issues and solutions
- Site-specific blocking and bypasses
- Troubleshooting common problems
- Advanced topics: MCP, AI agents, parallel contexts
- 快速入门与安装
- 端到端测试模式与断言
- 选择器策略与最佳实践
- 配置与Docker部署
- Docker与CI
- 调试方法与性能分析
- 常见自动化模式
- 隐身模式与反机器人绕过
- 已知问题与解决方案
- 站点特定拦截与绕过方案
- 常见问题排查
- 高级主题:MCP、AI Agent、并行上下文