Playwright Test 与 Reporter API
本附录列出 Playwright Test Runner 的配置、Fixture、Reporter 等 API。
Test 函数
| 方法 | 说明 |
|---|---|
test(name, fn) | 定义测试 |
test.describe(name, fn) | 定义测试分组 |
test.describe.serial(name, fn) | 串行测试分组 |
test.describe.parallel(name, fn) | 并行测试分组 |
test.describe.configure(options) | 配置分组行为(mode、retries) |
test.skip(name?, fn?) | 跳过测试 |
test.fail(name?, fn?) | 标记为预期失败 |
test.fixme(name?, fn?) | 标记为待修复 |
test.slow(name?, fn?) | 标记为慢测试(超时×3) |
test.only(name, fn) | 只运行此测试 |
test.beforeEach(fn) | 每个测试前执行 |
test.afterEach(fn) | 每个测试后执行 |
test.beforeAll(fn) | 所有测试前执行一次 |
test.afterAll(fn) | 所有测试后执行一次 |
test.extend(fixtures) | 创建自定义 Fixture |
test.use(options) | 覆盖当前作用域的配置 |
test.setTimeout(ms) | 设置当前测试的超时 |
test.step(name, fn) | 定义测试步骤(报告中可见) |
test.info() | 获取 TestInfo 对象 |
TestConfig 配置接口
| 字段 | 类型 | 说明 |
|---|---|---|
testDir | string | 测试目录 |
fullyParallel | boolean | 完全并行 |
workers | number | Worker 数量 |
retries | number | 重试次数 |
timeout | number | 测试总超时 |
reporter | string | array | 报告器 |
forbidOnly | boolean | 禁止 only |
testMatch | string | RegExp | 测试匹配模式 |
testIgnore | string | RegExp | 忽略模式 |
projects | Project[] | 项目列表 |
webServer | object | Web 服务器配置 |
globalSetup | string | 全局 Setup 脚本路径 |
globalTeardown | string | 全局 Teardown 脚本路径 |
use | object | 共享配置 |
expect | object | 断言配置 |
TestInfo 接口
| 字段/方法 | 说明 |
|---|---|
testInfo.title | 测试名称 |
testInfo.file | 测试文件路径 |
testInfo.line | 测试所在行号 |
testInfo.column | 测试所在列号 |
testInfo.expectedStatus | 预期状态:'passed' | 'failed' | 'skipped' |
testInfo.timeout | 当前测试超时 |
testInfo.annotations | 注解列表 |
testInfo.attachments | 附件列表 |
testInfo.project.name | 当前项目名称 |
testInfo.retry | 当前重试次数 |
testInfo.parallelIndex | 并行索引 |
testInfo.skip(reason?) | 跳过测试(动态) |
testInfo.fail(reason?) | 标记失败(动态) |
testInfo.attach(name, options?) | 附加文件到报告 |
Fixtures 接口
| Fixture | 类型 | 说明 |
|---|---|---|
page | Page | 浏览器标签页 |
context | BrowserContext | 浏览器上下文 |
browser | Browser | 浏览器实例 |
browserName | string | 浏览器名称 |
request | APIRequestContext | HTTP 请求客户端 |
WorkerInfo 接口
| 字段 | 说明 |
|---|---|
workerInfo.project.name | 项目名称 |
workerInfo.parallelIndex | Worker 并行索引 |
workerInfo.config | FullConfig 对象 |
Reporter 接口
| 方法 | 说明 |
|---|---|
reporter.onBegin(config, suite) | 测试开始 |
reporter.onTestBegin(test) | 单个测试开始 |
reporter.onTestEnd(test, result) | 单个测试结束 |
reporter.onStepBegin(test, result, step) | 步骤开始 |
reporter.onStepEnd(test, result, step) | 步骤结束 |
reporter.onEnd(result) | 测试结束 |
reporter.onExit() | 进程退出 |
reporter.printsToStdio() | 是否输出到标准输出 |
Suite 类
| 字段 | 说明 |
|---|---|
suite.title | Suite 名称 |
suite.suites | 子 Suite 列表 |
suite.tests | 测试列表 |
suite.location | Suite 位置 |
TestCase 类
| 字段 | 说明 |
|---|---|
test.title | 测试名称 |
test.location | 测试位置 |
test.expectedStatus | 预期状态 |
test.annotations | 注解列表 |
test.parent | 所属 Suite |
test.id() | 测试 ID |
TestResult 类
| 字段 | 说明 |
|---|---|
result.status | 状态:'passed' | 'failed' | 'skipped' | 'interrupted' |
result.duration | 执行时长(毫秒) |
result.error | 错误信息(TestError) |
result.attachments | 附件列表 |
result.stdout | 标准输出 |
result.stderr | 标准错误 |
Electron API
| 方法 | 说明 |
|---|---|
electron.launch(options?) | 启动 Electron 应用 |
ElectronApplication 类
| 方法 | 说明 |
|---|---|
electronApp.firstWindow() | 获取第一个窗口 |
electronApp.windows() | 获取所有窗口 |
electronApp.close() | 关闭应用 |
Android API
| 方法/类 | 说明 |
|---|---|
android.devices() | 获取连接的设备列表 |
AndroidDevice | Android 设备对象 |
AndroidWebView | WebView 对象 |
