Skip to content

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 配置接口 ​

字段类型说明
testDirstring测试目录
fullyParallelboolean完全并行
workersnumberWorker 数量
retriesnumber重试次数
timeoutnumber测试总超时
reporterstringarray
forbidOnlyboolean禁止 only
testMatchstringRegExp
testIgnorestringRegExp
projectsProject[]项目列表
webServerobjectWeb 服务器配置
globalSetupstring全局 Setup 脚本路径
globalTeardownstring全局 Teardown 脚本路径
useobject共享配置
expectobject断言配置

TestInfo 接口 ​

字段/方法说明
testInfo.title测试名称
testInfo.file测试文件路径
testInfo.line测试所在行号
testInfo.column测试所在列号
testInfo.expectedStatus预期状态:'passed'
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类型说明
pagePage浏览器标签页
contextBrowserContext浏览器上下文
browserBrowser浏览器实例
browserNamestring浏览器名称
requestAPIRequestContextHTTP 请求客户端

WorkerInfo 接口 ​

字段说明
workerInfo.project.name项目名称
workerInfo.parallelIndexWorker 并行索引
workerInfo.configFullConfig 对象

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.titleSuite 名称
suite.suites子 Suite 列表
suite.tests测试列表
suite.locationSuite 位置

TestCase 类 ​

字段说明
test.title测试名称
test.location测试位置
test.expectedStatus预期状态
test.annotations注解列表
test.parent所属 Suite
test.id()测试 ID

TestResult 类 ​

字段说明
result.status状态:'passed'
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()获取连接的设备列表
AndroidDeviceAndroid 设备对象
AndroidWebViewWebView 对象

AI 思考中...

Playwright 网络与输入 API

Playwright 其他 API

基于 VitePress 构建,部署于 GitHub Pages