Skip to content

参考

基于 Playwright v1.61 编写

速查

Locator

API说明
getByRole(role, { name })ARIA role + 名称(首选)
getByLabel / getByPlaceholder表单
getByText / getByTestId文本 / data-testid
.filter({ hasText, has })过滤
.and() / .or() / .nth(n)组合 / 位置

常用断言

toBeVisible / toBeHidden / toHaveText / toContainText / toHaveValue / toBeEnabled / toBeChecked / toHaveCount / toHaveAttribute / toHaveURL / toHaveTitle

CLI

bash
npx playwright test                  # 跑全部
npx playwright test --project=webkit # 指定浏览器
npx playwright test --ui             # UI Mode
npx playwright test --debug          # 调试
npx playwright test --workers=50%    # 并行度
npx playwright test --shard=1/4      # 分片
npx playwright codegen <url>         # 录制生成代码
npx playwright show-trace x.zip      # 看 trace
npx playwright install --with-deps   # 装浏览器(CI)

常用配置项

配置说明
testDir测试目录
use.baseURLpage.goto 前缀
use.tracetrace 录制策略
fullyParallel文件内并行
forbidOnlyCI 禁止 test.only
retries失败重试
projects多浏览器矩阵
webServer自动启动应用

近期版本特性

版本特性
v1.61page.localStorage / sessionStorage;video 新模式
v1.59page.ariaSnapshot() ARIA 树快照
v1.57改用 Chrome for Testing 构建
v1.52toContainClass();per-project workers

官方资源