用途:该技能面向 Web 应用的端到端(E2E)测试,适合编写浏览器测试、跨 Chromium/Firefox/WebKit 测试、视觉回归测试、E2E 网络请求模拟以及移动端视口测试。单元测试、Python 测试、测试质量分析、纯 API 契约测试等场景建议改用其他技能。
主要能力:跨浏览器测试;自动等待与自动重试以减少不稳定测试;并行执行与浏览器上下文隔离;TypeScript 优先,支持 async/await;可在 Windows、macOS、Linux 上运行。
安装与配置:可用 bun create playwright 初始化,或 bun add --dev @playwright/test 手动安装,再用 bunx playwright install 安装浏览器,Linux 上可用 bunx playwright install --with-deps 安装系统依赖,bunx playwright --version 验证。配置写在 playwright.config.ts,可设置 testDir、timeout、baseURL、trace 以及 projects 中的浏览器设备。
常用命令:bunx playwright test 运行全部测试;bunx playwright test tests/login.spec.ts 运行指定文件;--headed 显示浏览器;--debug 调试模式;--project=chromium 指定浏览器;--ui 打开 UI 模式;bunx playwright codegen http://localhost:3000 录制测试;bunx playwright show-report 查看报告;bunx playwright show-trace trace.zip 查看追踪;--update-snapshots 更新快照。
编写方式:使用 @playwright/test 的 test 与 expect,通过 page.goto 导航,用 getByText、getByRole、getByLabel、getByPlaceholder、getByTestId 等定位器操作元素,也可用 CSS/XPath 与链式过滤。断言包括 toHaveTitle、toHaveURL、toBeVisible、toBeEnabled、toHaveText、toContainText、toHaveAttribute、toHaveClass、toHaveValue、toBeEmpty、toHaveCount 及 not 取反。
限制:资料未提供收费、价格、平台支持范围之外的信息,也未提供具体版本要求;更多高级模式与最佳实践需参考 REFERENCE.md 及官方文档。