TestInfo
TestInfo
包含有关当前正在运行的测试的信息。它可用于测试函数、test.beforeEach()、test.afterEach()、test.beforeAll() 和 test.afterAll() 钩子,以及测试范围的 fixtures。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'));
});
方法
attach
新增于: v1.10将一个值或磁盘文件附加到当前测试。一些报告器会显示测试附件。必须指定 path 或 body,但不能同时指定两者。
例如,您可以将屏幕截图附加到测试
import { test, expect } from '@playwright/test';
test('basic test', async ({ page }, testInfo) => {
await page.goto('https://playwright.net.cn');
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()
新增于: v1.10将当前正在运行的测试标记为“应该失败”。Playwright Test 会运行此测试并确保它确实失败。这对于文档很有用,可以指出某些功能在修复之前是损坏的。这类似于 test.fail()。
用法
testInfo.fail();
fail(condition)
新增于: v1.10根据条件将当前正在运行的测试标记为“应该失败”,并带有可选描述。这类似于 test.fail()。
用法
testInfo.fail(condition);
testInfo.fail(condition, description);
参数
fixme()
新增于: v1.10将测试标记为“待修复 (fixme)”,表示有意修复它。测试会立即中止。这类似于 test.fixme()。
用法
testInfo.fixme();
fixme(condition)
新增于: v1.10根据条件将当前正在运行的测试标记为“待修复 (fixme)”,并带有可选描述。这类似于 test.fixme()。
用法
testInfo.fixme(condition);
testInfo.fixme(condition, description);
参数
-
当 condition 为
true
时,测试被标记为“待修复 (fixme)”。 -
可选描述,将反映在测试报告中。
outputPath
新增于: 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')
。但是,对于每个测试(即test-results/a-test-title
),此路径必须保留在 testInfo.outputDir 目录内,否则将抛出错误。
用法
testInfo.outputPath(...pathSegments);
参数
返回
setTimeout
新增于: 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()
新增于: v1.10无条件跳过当前正在运行的测试。测试会立即中止。这类似于 test.skip()。
用法
testInfo.skip();
skip(condition)
新增于: v1.10根据条件跳过当前正在运行的测试,并带有可选描述。这类似于 test.skip()。
用法
testInfo.skip(condition);
testInfo.skip(condition, description);
参数
slow()
新增于: v1.10将当前正在运行的测试标记为“慢速 (slow)”,给予它默认超时的三倍。这类似于 test.slow()。
用法
testInfo.slow();
slow(condition)
新增于: v1.10根据条件将当前正在运行的测试标记为“慢速 (slow)”,并带有可选描述,给予它默认超时的三倍。这类似于 test.slow()。
用法
testInfo.slow(condition);
testInfo.slow(condition, description);
参数
snapshotPath
新增于: v1.10使用给定的 pathSegments
返回快照文件的路径。了解更多关于快照的信息。
请注意,
pathSegments
接受指向快照文件的路径段,例如testInfo.snapshotPath('relative', 'path', 'to', 'snapshot.png')
。但是,对于每个测试文件(即a.spec.js-snapshots
),此路径必须保留在快照目录内,否则将抛出错误。
用法
testInfo.snapshotPath(...pathSegments);
参数
返回
属性
annotations
新增于: v1.10适用于当前测试的注解列表。包括来自测试的注解、来自测试所属的所有 test.describe() 组的注解以及测试文件的文件级注解。
了解更多关于测试注解的信息。
用法
testInfo.annotations
类型
attachments
新增于: v1.10附加到当前测试的文件或缓冲区列表。一些报告器会显示测试附件。
要添加附件,请使用 testInfo.attach(),而不是直接推送到此数组。
用法
testInfo.attachments
类型
column
新增于: v1.10声明当前正在运行测试的列号。
用法
testInfo.column
类型
config
新增于: v1.10来自配置文件的已处理配置。
用法
testInfo.config
类型
duration
新增于: v1.10测试完成所需的时间,单位为毫秒。在测试完成之前(无论成功与否)始终为零。可在 test.afterEach() 钩子中使用。
用法
testInfo.duration
类型
error
新增于: v1.10如果在测试执行期间抛出任何错误,则为第一个错误。这等同于 testInfo.errors 中的第一个元素。
用法
testInfo.error
类型
errors
新增于: v1.10如果在测试执行期间抛出任何错误,则为这些错误。
用法
testInfo.errors
类型
expectedStatus
新增于: v1.10当前正在运行测试的预期状态。通常是 'passed'
,但有少数例外情况
- - 对于跳过的测试,例如使用 test.skip(),状态为
'skipped'
; - - 对于使用 test.fail() 标记为失败的测试,状态为
'failed'
。
预期状态通常与实际 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"
file
新增于: v1.10声明当前正在运行测试的文件绝对路径。
用法
testInfo.file
类型
fn
新增于: v1.10传递给 test(title, testFunction)
的测试函数。
用法
testInfo.fn
类型
line
新增于: v1.10声明当前正在运行测试的行号。
用法
testInfo.line
类型
outputDir
新增于: v1.10此特定测试运行的输出目录绝对路径。每次测试运行都有自己的目录,因此不会冲突。
用法
testInfo.outputDir
类型
parallelIndex
新增于: v1.10Worker 的索引,介于 0
和 workers - 1
之间。保证同时运行的 worker 具有不同的 parallelIndex
。当 worker 重启时(例如在失败后),新的 worker 进程具有相同的 parallelIndex
。
也可通过 process.env.TEST_PARALLEL_INDEX
访问。了解更多关于 Playwright Test 的并行和分片。
用法
testInfo.parallelIndex
类型
project
新增于: v1.10来自配置文件的已处理项目配置。
用法
testInfo.project
类型
repeatEachIndex
新增于: v1.10在“重复每个”模式下运行时指定唯一的重复索引。通过将 --repeat-each
传递给命令行来启用此模式。
用法
testInfo.repeatEachIndex
类型
retry
新增于: v1.10指定测试失败后重试的次数。第一次测试运行的 testInfo.retry 等于零,第一次重试等于一,依此类推。了解更多关于重试的信息。
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
新增于: v1.10此特定测试的快照输出目录绝对路径。每个测试套件都有自己的目录,因此不会冲突。
此属性不考虑 testProject.snapshotPathTemplate 配置。
用法
testInfo.snapshotDir
类型
snapshotSuffix
新增于: v1.10不建议使用 testInfo.snapshotSuffix。请使用 testConfig.snapshotPathTemplate 配置快照路径。
用于区分不同测试配置之间快照的后缀。例如,如果快照依赖于平台,您可以将 testInfo.snapshotSuffix
设置为 process.platform
。在这种情况下,expect(value).toMatchSnapshot(snapshotName)
将根据平台使用不同的快照。了解更多关于快照的信息。
用法
testInfo.snapshotSuffix
类型
status
新增于: 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"
tags
新增于: v1.43应用于测试的标签。了解更多关于标签的信息。
测试运行时对此列表所做的任何更改对测试报告器不可见。
用法
testInfo.tags
类型
testId
新增于: v1.32与报告器 API 中测试用例 id 相匹配的测试 id。
用法
testInfo.testId
类型
timeout
新增于: 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
类型
title
新增于: v1.10传递给 test(title, testFunction)
的当前正在运行测试的标题。
用法
testInfo.title
类型
titlePath
新增于: v1.10从测试文件名开始的完整标题路径。
用法
testInfo.titlePath
类型
workerIndex
新增于: v1.10运行测试的 worker 进程的唯一索引。当 worker 重启时(例如在失败后),新的 worker 进程会获得一个新的唯一 workerIndex
。
也可通过 process.env.TEST_WORKER_INDEX
访问。了解更多关于 Playwright Test 的并行和分片。
用法
testInfo.workerIndex
类型