跳到正文

TestInfo ​

Added in: v1.10

TestInfo 包含有关当前运行测试的信息。它对测试函数、test.beforeEach()、test.afterEach()、test.beforeAll() 和 test.afterAll() 钩子以及测试范围的 fixtures 可用。TestInfo 提供用于控制测试执行的实用程序:附加文件、更新测试超时、确定当前正在运行哪个测试以及它是否被重试等。

js
import { test, expect } from '@playwright/test';

test('basic test', async ({ page }, testInfo) => {
  expect(testInfo.title).toBe('basic test');
  await page.screenshot({ path: testInfo.outputPath('screenshot.png') });
});

方法 ​

testInfo.attach() ​

Added in: v1.10 async

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

例如,你可以将屏幕截图附加到测试:

js
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 返回的文件:

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

INFO

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

参数

  • 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 互斥。

testInfo.fail() ​

Added in: v1.10

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

testInfo.fail() ​

Added in: v1.10

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

参数

  • condition boolean

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

  • description string (可选)

    将反映在测试报告中的可选描述。

testInfo.fixme() ​

Added in: v1.10

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

testInfo.fixme() ​

Added in: v1.10

有条件地将当前运行的测试标记为 "fixme",可选地带有描述。这类似于 test.fixme()。

参数

  • condition boolean

    当条件为 true 时,测试被标记为 "fixme"。

  • description string (可选)

    将反映在测试报告中的可选描述。

testInfo.outputPath() ​

Added in: v1.10

返回 testInfo.outputDir 内的路径,测试可以安全地在其中放置临时文件。保证并行运行的测试不会相互干扰。

js
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),否则会抛出错误。

参数

  • ...pathSegments Array<string>

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

返回值

  • string

testInfo.setTimeout() ​

Added in: v1.10

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

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

js
import { test, expect } from '@playwright/test';

test.beforeEach(async ({ page }, testInfo) => {
  // 将运行此钩子的所有测试的超时时间延长 30 秒。
  testInfo.setTimeout(testInfo.timeout + 30000);
});

参数

  • timeout int

    超时时间(毫秒)。

testInfo.skip() ​

Added in: v1.10

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

testInfo.skip() ​

Added in: v1.10

有条件地跳过当前运行的测试,可选地带有描述。这类似于 test.skip()。

参数

  • condition boolean

    跳过条件。当条件为 true 时跳过测试。

  • description string (可选)

    将反映在测试报告中的可选描述。

testInfo.slow() ​

Added in: v1.10

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

testInfo.slow() ​

Added in: v1.10

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

参数

  • condition boolean

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

  • description string (可选)

    将反映在测试报告中的可选描述。

testInfo.snapshotPath() ​

Added in: v1.10

返回给定 name 的快照文件路径。传递 kind 以获取特定路径:

用法

js
await expect(page).toHaveScreenshot('header.png');
// 上面的屏幕截图断言期望屏幕截图在此路径:
const screenshotPath = test.info().snapshotPath('header.png', { kind: 'screenshot' });

await expect(page.getByRole('main')).toMatchAriaSnapshot({ name: 'main.aria.yml' });
// 上面的 Aria 快照断言期望快照在此路径:
const ariaSnapshotPath = test.info().snapshotPath('main.aria.yml', { kind: 'aria' });

expect('some text').toMatchSnapshot('snapshot.txt');
// 上面的快照断言期望快照在此路径:
const snapshotPath = test.info().snapshotPath('snapshot.txt');

expect('some text').toMatchSnapshot(['dir', 'subdir', 'snapshot.txt']);
// 上面的快照断言期望快照在此路径:
const nestedPath = test.info().snapshotPath('dir', 'subdir', 'snapshot.txt');

参数

  • ...name Array<string>

    快照的名称或定义快照文件路径的路径段。同一测试文件中具有相同名称的快照应该是相同的。

    当传递 kind 时,不支持多个名称段。

  • options Object (可选)

    • kind "snapshot" | "screenshot" | "aria" (可选) Added in: v1.53

      快照类型控制使用哪个快照路径模板。有关更多详情,请参阅 testConfig.snapshotPathTemplate。默认为 'snapshot'。

返回值

  • string

属性 ​

testInfo.annotations ​

Added in: v1.10

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

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

类型

  • Array<Object>

testInfo.attachments ​

Added in: v1.10

附加到当前测试的文件或缓冲区列表。一些报告器会显示测试附件。

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

类型

  • Array<Object>

testInfo.column ​

Added in: v1.10

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

类型

  • int

testInfo.config ​

Added in: v1.10

来自配置文件的已处理配置。

类型

  • FullConfig

testInfo.duration ​

Added in: v1.10

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

类型

  • int

testInfo.error ​

Added in: v1.10

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

类型

  • TestInfoError

testInfo.errors ​

Added in: v1.10

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

类型

  • Array<TestInfoError>

testInfo.expectedStatus ​

Added in: v1.10

当前运行测试的预期状态。通常是 'passed',除了少数情况:

  • 'skipped' 用于跳过的测试,例如使用 test.skip();
  • 'failed' 用于使用 test.fail() 标记为失败的测试。

预期状态通常与实际的 testInfo.status 进行比较:

js
import { test, expect } from '@playwright/test';

test.afterEach(async ({}, testInfo) => {
  if (testInfo.status !== testInfo.expectedStatus)
    console.log(`${testInfo.title} did not run as expected!`);
});

类型

  • "passed" | "failed" | "timedOut" | "skipped" | "interrupted"

testInfo.file ​

Added in: v1.10

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

类型

  • string

testInfo.fn ​

Added in: v1.10

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

类型

  • function()

testInfo.line ​

Added in: v1.10

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

类型

  • int

testInfo.outputDir ​

Added in: v1.10

此特定测试运行的输出目录的绝对路径。每个测试运行都有自己的目录,因此它们不会冲突。

类型

  • string

testInfo.parallelIndex ​

Added in: v1.10

0 到 workers - 1 之间的 worker 索引。保证同时运行的 worker 具有不同的 parallelIndex。当 worker 重启时,例如在失败后,新的 worker 进程具有相同的 parallelIndex。

也可作为 process.env.TEST_PARALLEL_INDEX 使用。了解更多关于使用 Playwright Test 的并行和分片。

类型

  • int

testInfo.project ​

Added in: v1.10

来自配置文件的已处理项目配置。

类型

  • FullProject

testInfo.repeatEachIndex ​

Added in: v1.10

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

类型

  • int

testInfo.retry ​

Added in: v1.10

指定测试在失败后重试时的重试次数。第一次测试运行时 testInfo.retry 等于零,第一次重试时等于一,依此类推。了解更多关于重试的信息。

js
import { test, expect } from '@playwright/test';

test.beforeEach(async ({}, testInfo) => {
  // 你可以在任何钩子或 fixture 中访问 testInfo.retry。
  if (testInfo.retry > 0)
    console.log(`Retrying!`);
});

test('my test', async ({ page }, testInfo) => {
  // 这里我们在重试时清除一些服务器端状态。
  if (testInfo.retry)
    await cleanSomeCachesOnTheServer();
  // ...
});

类型

  • int

testInfo.snapshotDir ​

Added in: v1.10

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

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

类型

  • string

testInfo.snapshotSuffix ​

Added in: v1.10

INFO

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

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

类型

  • string

testInfo.status ​

Added in: v1.10

当前运行测试的实际状态。在测试完成后的 test.afterEach() 钩子和 fixtures 中可用。

状态通常与 testInfo.expectedStatus 进行比较:

js
import { test, expect } from '@playwright/test';

test.afterEach(async ({}, testInfo) => {
  if (testInfo.status !== testInfo.expectedStatus)
    console.log(`${testInfo.title} did not run as expected!`);
});

类型

  • "passed" | "failed" | "timedOut" | "skipped" | "interrupted"

testInfo.tags ​

Added in: v1.43

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

INFO

在测试运行期间对此列表所做的任何更改将对测试报告器不可见。

类型

  • Array<string>

testInfo.testId ​

Added in: v1.32

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

类型

  • string

testInfo.timeout ​

Added in: v1.10

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

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

js
import { test, expect } from '@playwright/test';

test.beforeEach(async ({ page }, testInfo) => {
  // 将运行此钩子的所有测试的超时时间延长 30 秒。
  testInfo.setTimeout(testInfo.timeout + 30000);
});

类型

  • int

testInfo.title ​

Added in: v1.10

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

类型

  • string

testInfo.titlePath ​

Added in: v1.10

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

类型

  • Array<string>

testInfo.workerIndex ​

Added in: v1.10

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

也可作为 process.env.TEST_WORKER_INDEX 使用。了解更多关于使用 Playwright Test 的并行和分片。

类型

  • int

基于 Apache 2.0 许可证发布。