TestInfo
TestInfo
包含当前运行测试的相关信息。它可用于测试函数、test.beforeEach()、test.afterEach()、test.beforeAll() 和 test.afterAll() 钩子函数,以及测试作用域的固件。TestInfo
提供了控制测试执行的实用工具:附加文件、更新测试超时时间、确定当前运行的测试以及是否进行了重试等。
import { test, expect } from '@playwright/test';
test('basic test', async ({ page }, testInfo) => {
expect(testInfo.title).toBe('basic test');
await page.screenshot(testInfo.outputPath('screenshot.png'));
});
方法
附加
Added in: v1.10将某个值或磁盘上的文件附加到当前测试中。某些报告器会显示测试附件。必须指定path或body其中之一,但不能同时指定两者。
例如,您可以将截图附加到测试中:
import { test, expect } from '@playwright/test';
test('basic test', async ({ page }, testInfo) => {
await page.goto('https://playwright.dev');
const screenshot = await page.screenshot();
await testInfo.attach('screenshot', { body: screenshot, contentType: 'image/png' });
});
或者你可以附加由你的API返回的文件:
import { test, expect } from '@playwright/test';
import { download } from './my-custom-helpers';
test('basic test', async ({}, testInfo) => {
const tmpPath = await download('a');
await testInfo.attach('downloaded', { path: tmpPath });
});
testInfo.attach() 会自动将附加文件复制到报告程序可访问的位置。在等待attach调用完成后,您可以安全地删除附件。
用法
await testInfo.attach(name);
await testInfo.attach(name, options);
参数
-
附件名称。该名称会被规范化处理,并在保存到磁盘时用作文件名的前缀。
-
options
Object (可选)
返回
fail()
Added in: v1.10将当前运行的测试标记为"预期失败"。Playwright Test会运行此测试并确保它确实失败。这对于文档目的很有用,可以确认某些功能在修复之前是损坏的。这与test.fail()类似。
用法
testInfo.fail();
fail(condition)
Added in: v1.10有条件地将当前运行的测试标记为"应失败",并可选择添加描述。这与test.fail()类似。
用法
testInfo.fail(condition);
testInfo.fail(condition, description);
参数
fixme()
Added in: v1.10将测试标记为"待修复",目的是稍后修复它。测试会立即中止。这与test.fixme()类似。
用法
testInfo.fixme();
待修复(condition)
Added in: v1.10有条件地将当前运行的测试标记为"待修复",并可选择添加描述。这与test.fixme()类似。
用法
testInfo.fixme(condition);
testInfo.fixme(condition, description);
参数
outputPath
Added in: v1.10返回testInfo.outputDir内的一个路径,测试可以安全地在该路径下存放临时文件。确保并行运行的测试不会相互干扰。
import { test, expect } from '@playwright/test';
import fs from 'fs';
test('example test', async ({}, testInfo) => {
const file = testInfo.outputPath('dir', 'temporary-file.txt');
await fs.promises.writeFile(file, 'Put some data to the dir/temporary-file.txt', 'utf8');
});
请注意,
pathSegments
接受指向测试输出目录的路径段,例如testInfo.outputPath('relative', 'path', 'to', 'output')
。 但此路径必须位于每个测试的testInfo.outputDir目录内(即test-results/a-test-title
),否则会抛出异常。
用法
testInfo.outputPath(...pathSegments);
参数
返回
setTimeout
Added in: v1.10更改当前运行测试的超时时间。零表示无超时限制。了解更多关于各种超时的信息。
超时时间通常在配置文件中指定,但在某些场景下更改超时时间可能很有用:
import { test, expect } from '@playwright/test';
test.beforeEach(async ({ page }, testInfo) => {
// Extend timeout for all tests running this hook by 30 seconds.
testInfo.setTimeout(testInfo.timeout + 30000);
});
用法
testInfo.setTimeout(timeout);
参数
skip()
Added in: v1.10无条件跳过当前运行的测试。测试会立即中止。这与test.skip()类似。
用法
testInfo.skip();
skip(condition)
Added in: v1.10有条件地跳过当前运行的测试,并可选择添加描述。这与test.skip()类似。
用法
testInfo.skip(condition);
testInfo.skip(condition, description);
参数
slow()
Added in: v1.10将当前运行的测试标记为"慢速",给予其三倍的默认超时时间。这与test.slow()类似。
用法
testInfo.slow();
slow(condition)
Added in: v1.10有条件地将当前运行的测试标记为"慢速",可附带可选描述,使其获得默认超时时间的三倍。这与test.slow()类似。
用法
testInfo.slow(condition);
testInfo.slow(condition, description);
参数
snapshotPath
Added in: v1.10返回一个包含给定pathSegments
的快照文件路径。了解更多关于snapshots的信息。
请注意,
pathSegments
接受指向快照文件的路径段,例如testInfo.snapshotPath('relative', 'path', 'to', 'snapshot.png')
。 但此路径必须保持在每个测试文件的快照目录内(即a.spec.js-snapshots
),否则会抛出错误。
用法
testInfo.snapshotPath(...pathSegments);
参数
返回
属性
注解
Added in: v1.10适用于当前测试的注解列表。包括来自测试的注解、测试所属的所有test.describe()组的注解以及测试文件的文件级注解。
了解更多关于测试注解的信息。
用法
testInfo.annotations
类型
附件
Added in: v1.10当前测试所附加的文件或缓冲区列表。部分报告器会显示测试附件。
要添加附件,请使用testInfo.attach()而不是直接推送到此数组。
用法
testInfo.attachments
类型
列
Added in: v1.10当前运行的测试声明所在的列号。
用法
testInfo.column
类型
配置
Added in: v1.10从配置文件处理后的配置。
用法
testInfo.config
类型
持续时间
Added in: v1.10测试完成所用的毫秒数。在测试完成之前(无论成功与否)始终为零。可以在test.afterEach()钩子中使用。
用法
testInfo.duration
类型
错误
Added in: v1.10测试执行期间抛出的第一个错误(如果有的话)。这等同于testInfo.errors中的第一个元素。
用法
testInfo.error
类型
错误
Added in: v1.10测试执行期间抛出的错误(如果有的话)。
用法
testInfo.errors
类型
expectedStatus
Added in: v1.10当前运行测试的预期状态。通常为'passed'
,除以下几种情况外:
'skipped'
表示跳过的测试,例如使用 test.skip();'failed'
表示被标记为失败的测试,使用 test.fail()。
预期状态通常与实际testInfo.status进行比较:
import { test, expect } from '@playwright/test';
test.afterEach(async ({}, testInfo) => {
if (testInfo.status !== testInfo.expectedStatus)
console.log(`${testInfo.title} did not run as expected!`);
});
用法
testInfo.expectedStatus
类型
- "passed" | "failed" | "timedOut" | "skipped" | "interrupted"
文件
Added in: v1.10当前运行测试声明文件的绝对路径。
用法
testInfo.file
类型
fn
Added in: v1.10作为参数传递给 test(title, testFunction)
的测试函数。
用法
testInfo.fn
类型
行
Added in: v1.10当前运行的测试声明所在的行号。
用法
testInfo.line
类型
outputDir
Added in: v1.10本次特定测试运行的输出目录绝对路径。每个测试运行都有自己独立的目录,因此不会发生冲突。
用法
testInfo.outputDir
类型
parallelIndex
Added in: v1.10工作线程的索引值介于0
到workers - 1
之间。可以保证同时运行的工作线程具有不同的parallelIndex
。当工作线程重启时(例如发生故障后),新工作进程会保持相同的parallelIndex
。
也可通过 process.env.TEST_PARALLEL_INDEX
获取。了解更多关于 Playwright Test 的并行与分片机制。
用法
testInfo.parallelIndex
类型
项目
Added in: v1.10从配置文件处理的项目配置。
用法
testInfo.project
类型
repeatEachIndex
Added in: v1.10指定在"重复每个"模式下运行时的唯一重复索引。通过向命令行传递--repeat-each
参数可启用此模式。
用法
testInfo.repeatEachIndex
类型
重试
Added in: v1.10指定测试失败后重试时的重试次数。首次测试运行时testInfo.retry值为零,第一次重试时值为一,依此类推。了解更多关于retries的信息。
import { test, expect } from '@playwright/test';
test.beforeEach(async ({}, testInfo) => {
// You can access testInfo.retry in any hook or fixture.
if (testInfo.retry > 0)
console.log(`Retrying!`);
});
test('my test', async ({ page }, testInfo) => {
// Here we clear some server-side state when retrying.
if (testInfo.retry)
await cleanSomeCachesOnTheServer();
// ...
});
用法
testInfo.retry
类型
snapshotDir
Added in: v1.10此特定测试的快照输出目录的绝对路径。每个测试套件都有自己的目录,因此不会发生冲突。
此属性未考虑testProject.snapshotPathTemplate配置。
用法
testInfo.snapshotDir
类型
snapshotSuffix
Added in: v1.10不建议使用testInfo.snapshotSuffix。请使用testConfig.snapshotPathTemplate来配置快照路径。
用于区分多个测试配置之间快照的后缀。例如,如果快照依赖于平台,您可以将testInfo.snapshotSuffix
设置为等于process.platform
。在这种情况下,expect(value).toMatchSnapshot(snapshotName)
将根据平台使用不同的快照。了解更多关于snapshots的信息。
用法
testInfo.snapshotSuffix
类型
状态
Added in: v1.10当前运行测试的实际状态。在测试完成后,在test.afterEach()钩子和fixtures中可用。
状态通常与testInfo.expectedStatus进行比较:
import { test, expect } from '@playwright/test';
test.afterEach(async ({}, testInfo) => {
if (testInfo.status !== testInfo.expectedStatus)
console.log(`${testInfo.title} did not run as expected!`);
});
用法
testInfo.status
类型
- "passed" | "failed" | "timedOut" | "skipped" | "interrupted"
标签
Added in: v1.43适用于测试的标签。了解更多关于tags的信息。
测试运行时对此列表所做的任何更改都不会对测试报告者可见。
用法
testInfo.tags
类型
testId
Added in: v1.32测试ID与报告API中的测试用例ID匹配。
用法
testInfo.testId
类型
超时时间
Added in: v1.10当前运行测试的超时时间(毫秒)。零表示没有超时。了解更多关于各种超时的信息。
超时时间通常在配置文件中指定
import { test, expect } from '@playwright/test';
test.beforeEach(async ({ page }, testInfo) => {
// Extend timeout for all tests running this hook by 30 seconds.
testInfo.setTimeout(testInfo.timeout + 30000);
});
用法
testInfo.timeout
类型
标题
Added in: v1.10当前运行的测试标题,传递给 test(title, testFunction)
。
用法
testInfo.title
类型
titlePath
Added in: v1.10从测试文件名开始的完整标题路径。
用法
testInfo.titlePath
类型
workerIndex
Added in: v1.10运行测试的工作进程的唯一索引。当工作进程重启时,例如在失败后,新的工作进程会获得一个新的唯一workerIndex
。
也可通过 process.env.TEST_WORKER_INDEX
获取。了解更多关于使用 Playwright Test 进行并行测试和分片的信息。
用法
testInfo.workerIndex
类型