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);
参数
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')
。但是,此路径必须保留在每个测试的 testInfo.outputDir 目录(即test-results/a-test-title
)中,否则会抛出错误。
用法
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.10工作进程的索引,介于 0
和 workers - 1
之间。保证同时运行的工作进程具有不同的 parallelIndex
。当工作进程重新启动时(例如,在失败后),新的工作进程具有相同的 parallelIndex
。
也可作为 process.env.TEST_PARALLEL_INDEX
使用。了解有关 Playwright Test 的 并行性和分片的更多信息。
用法
testInfo.parallelIndex
类型
project
添加于: v1.10来自 配置文件 的已处理项目配置。
用法
testInfo.project
类型
repeatEachIndex
添加于: v1.10在“repeat each”模式下运行时,指定唯一的重复索引。此模式通过将 --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正在运行测试的工作进程的唯一索引。当工作进程重新启动时(例如,在失败后),新的工作进程会获得一个新的唯一 workerIndex
。
也可作为 process.env.TEST_WORKER_INDEX
使用。了解有关 Playwright Test 的 并行性和分片的更多信息。
用法
testInfo.workerIndex
类型