跳至主要内容

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 testInfo.attach

将某个值或磁盘上的文件附加到当前测试中。某些报告器会显示测试附件。必须指定pathbody其中之一,但不能同时指定两者。

例如,您可以将截图附加到测试中:

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 });
});
note

testInfo.attach() 会自动将附加文件复制到报告程序可访问的位置。在等待attach调用完成后,您可以安全地删除附件。

用法

await testInfo.attach(name);
await testInfo.attach(name, options);

参数

  • name string#

    附件名称。该名称会被规范化处理,并在保存到磁盘时用作文件名的前缀。

  • options Object (可选)

    • body string | Buffer (可选)#

      附件正文。与path互斥。

    • contentType string (可选)#

      此附件的内容类型,用于在报告中正确显示,例如'application/json''image/png'。如果省略,内容类型将根据path推断,对于string附件默认为text/plain,对于Buffer附件默认为application/octet-stream

    • path string (可选)#

      文件系统中附件文件的路径。与body互斥。

返回


fail()

Added in: v1.10 testInfo.fail()

将当前运行的测试标记为"预期失败"。Playwright Test会运行此测试并确保它确实失败。这对于文档目的很有用,可以确认某些功能在修复之前是损坏的。这与test.fail()类似。

用法

testInfo.fail();

fail(condition)

Added in: v1.10 testInfo.fail(condition)

有条件地将当前运行的测试标记为"应失败",并可选择添加描述。这与test.fail()类似。

用法

testInfo.fail(condition);
testInfo.fail(condition, description);

参数

  • condition boolean#

    当条件为true时,测试会被标记为"应该失败"。

  • description string (可选)#

    可选描述,将在测试报告中体现。


fixme()

Added in: v1.10 testInfo.fixme()

将测试标记为"待修复",目的是稍后修复它。测试会立即中止。这与test.fixme()类似。

用法

testInfo.fixme();

待修复(condition)

Added in: v1.10 testInfo.fixme(condition)

有条件地将当前运行的测试标记为"待修复",并可选择添加描述。这与test.fixme()类似。

用法

testInfo.fixme(condition);
testInfo.fixme(condition, description);

参数

  • condition boolean#

    当条件为true时,测试被标记为"待修复"。

  • description string (可选)#

    可选描述,将在测试报告中体现。


outputPath

Added in: v1.10 testInfo.outputPath

返回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);

参数

  • ...pathSegments Array<string>#

    要在结果路径末尾追加的路径段。

返回


setTimeout

Added in: v1.10 testInfo.setTimeout

更改当前运行测试的超时时间。零表示无超时限制。了解更多关于各种超时的信息。

超时时间通常在配置文件中指定,但在某些场景下更改超时时间可能很有用:

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);

参数

  • timeout number#

    超时时间(毫秒)。


skip()

Added in: v1.10 testInfo.skip()

无条件跳过当前运行的测试。测试会立即中止。这与test.skip()类似。

用法

testInfo.skip();

skip(condition)

Added in: v1.10 testInfo.skip(condition)

有条件地跳过当前运行的测试,并可选择添加描述。这与test.skip()类似。

用法

testInfo.skip(condition);
testInfo.skip(condition, description);

参数

  • condition boolean#

    一个跳过条件。当条件为true时,测试将被跳过。

  • description string (可选)#

    可选描述,将在测试报告中体现。


slow()

Added in: v1.10 testInfo.slow()

将当前运行的测试标记为"慢速",给予其三倍的默认超时时间。这与test.slow()类似。

用法

testInfo.slow();

slow(condition)

Added in: v1.10 testInfo.slow(condition)

有条件地将当前运行的测试标记为"慢速",可附带可选描述,使其获得默认超时时间的三倍。这与test.slow()类似。

用法

testInfo.slow(condition);
testInfo.slow(condition, description);

参数

  • condition boolean#

    当条件为true时,测试会被标记为"慢速"。

  • description string (可选)#

    可选描述,将在测试报告中体现。


snapshotPath

Added in: v1.10 testInfo.snapshotPath

返回一个包含给定pathSegments的快照文件路径。了解更多关于snapshots的信息。

请注意,pathSegments接受指向快照文件的路径段,例如testInfo.snapshotPath('relative', 'path', 'to', 'snapshot.png')。 但此路径必须保持在每个测试文件的快照目录内(即a.spec.js-snapshots),否则会抛出错误。

用法

testInfo.snapshotPath(...pathSegments);

参数

  • ...pathSegments Array<string>#

    快照名称或用于定义快照文件路径的路径片段。同一测试文件中相同名称的快照预期应该是相同的。

返回


属性

注解

Added in: v1.10 testInfo.annotations

适用于当前测试的注解列表。包括来自测试的注解、测试所属的所有test.describe()组的注解以及测试文件的文件级注解。

了解更多关于测试注解的信息。

用法

testInfo.annotations

类型


附件

Added in: v1.10 testInfo.attachments

当前测试所附加的文件或缓冲区列表。部分报告器会显示测试附件。

要添加附件,请使用testInfo.attach()而不是直接推送到此数组。

用法

testInfo.attachments

类型

  • 数组<Object>
    • name string

      附件名称。

    • contentType string

      此附件的内容类型,用于在报告中正确显示,例如'application/json''image/png'

    • path string (可选)

      附加文件在文件系统中的可选路径。

    • body Buffer (可选)

      可选的附件内容,用于替代文件。


Added in: v1.10 testInfo.column

当前运行的测试声明所在的列号。

用法

testInfo.column

类型


配置

Added in: v1.10 testInfo.config

配置文件处理后的配置。

用法

testInfo.config

类型


持续时间

Added in: v1.10 testInfo.duration

测试完成所用的毫秒数。在测试完成之前(无论成功与否)始终为零。可以在test.afterEach()钩子中使用。

用法

testInfo.duration

类型


错误

Added in: v1.10 testInfo.error

测试执行期间抛出的第一个错误(如果有的话)。这等同于testInfo.errors中的第一个元素。

用法

testInfo.error

类型


错误

Added in: v1.10 testInfo.errors

测试执行期间抛出的错误(如果有的话)。

用法

testInfo.errors

类型


expectedStatus

Added in: v1.10 testInfo.expectedStatus

当前运行测试的预期状态。通常为'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

当前运行测试声明文件的绝对路径。

用法

testInfo.file

类型


fn

Added in: v1.10 testInfo.fn

作为参数传递给 test(title, testFunction) 的测试函数。

用法

testInfo.fn

类型


Added in: v1.10 testInfo.line

当前运行的测试声明所在的行号。

用法

testInfo.line

类型


outputDir

Added in: v1.10 testInfo.outputDir

本次特定测试运行的输出目录绝对路径。每个测试运行都有自己独立的目录,因此不会发生冲突。

用法

testInfo.outputDir

类型


parallelIndex

Added in: v1.10 testInfo.parallelIndex

工作线程的索引值介于0workers - 1之间。可以保证同时运行的工作线程具有不同的parallelIndex。当工作线程重启时(例如发生故障后),新工作进程会保持相同的parallelIndex

也可通过 process.env.TEST_PARALLEL_INDEX 获取。了解更多关于 Playwright Test 的并行与分片机制

用法

testInfo.parallelIndex

类型


项目

Added in: v1.10 testInfo.project

配置文件处理的项目配置。

用法

testInfo.project

类型


repeatEachIndex

Added in: v1.10 testInfo.repeatEachIndex

指定在"重复每个"模式下运行时的唯一重复索引。通过向命令行传递--repeat-each参数可启用此模式。

用法

testInfo.repeatEachIndex

类型


重试

Added in: v1.10 testInfo.retry

指定测试失败后重试时的重试次数。首次测试运行时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 testInfo.snapshotDir

此特定测试的快照输出目录的绝对路径。每个测试套件都有自己的目录,因此不会发生冲突。

此属性未考虑testProject.snapshotPathTemplate配置。

用法

testInfo.snapshotDir

类型


snapshotSuffix

Added in: v1.10 testInfo.snapshotSuffix
note

不建议使用testInfo.snapshotSuffix。请使用testConfig.snapshotPathTemplate来配置快照路径。

用于区分多个测试配置之间快照的后缀。例如,如果快照依赖于平台,您可以将testInfo.snapshotSuffix设置为等于process.platform。在这种情况下,expect(value).toMatchSnapshot(snapshotName)将根据平台使用不同的快照。了解更多关于snapshots的信息。

用法

testInfo.snapshotSuffix

类型


状态

Added in: v1.10 testInfo.status

当前运行测试的实际状态。在测试完成后,在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 testInfo.tags

适用于测试的标签。了解更多关于tags的信息。

note

测试运行时对此列表所做的任何更改都不会对测试报告者可见。

用法

testInfo.tags

类型


testId

Added in: v1.32 testInfo.testId

测试ID与报告API中的测试用例ID匹配。

用法

testInfo.testId

类型


超时时间

Added in: v1.10 testInfo.timeout

当前运行测试的超时时间(毫秒)。零表示没有超时。了解更多关于各种超时的信息。

超时时间通常在配置文件中指定

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 testInfo.title

当前运行的测试标题,传递给 test(title, testFunction)

用法

testInfo.title

类型


titlePath

Added in: v1.10 testInfo.titlePath

从测试文件名开始的完整标题路径。

用法

testInfo.titlePath

类型


workerIndex

Added in: v1.10 testInfo.workerIndex

运行测试的工作进程的唯一索引。当工作进程重启时,例如在失败后,新的工作进程会获得一个新的唯一workerIndex

也可通过 process.env.TEST_WORKER_INDEX 获取。了解更多关于使用 Playwright Test 进行并行测试和分片的信息。

用法

testInfo.workerIndex

类型