Skip to main content

TestInfo

TestInfo 包含有关当前运行测试的信息。它可用于任何测试函数、test.beforeEach(hookFunction)test.afterEach(hookFunction) 钩子以及测试范围的 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'));
});

testInfo.attach(name[, options])

Added in: v1.10
  • 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 互斥。#
  • returns: <Promise<void>>#

将值或磁盘上的文件附加到当前测试。某些报告器会显示测试附件。必须指定 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';

test('basic test', async ({}, testInfo) => {
const { download } = require('./my-custom-helpers');
const tmpPath = await download('a');
await testInfo.attach('downloaded', { path: tmpPath });
});
note

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

testInfo.fail()

Added in: v1.10

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

testInfo.fail(condition[, description])

Added in: v1.10
  • condition <boolean> 当条件为 true 时,测试被标记为 "should fail"。#
  • description? <string> 可选的描述,将反映在测试报告中。#
  • returns: <void>#

有条件地将当前运行的测试标记为 "should fail",并带有可选描述。这类似于 test.fail(condition[, description])

testInfo.fixme()

Added in: v1.10

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

testInfo.fixme(condition[, description])

Added in: v1.10
  • condition <boolean> 当条件为 true 时,测试被标记为 "fixme"。#
  • description? <string> 可选的描述,将反映在测试报告中。#
  • returns: <void>#

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

testInfo.outputPath(...pathSegments)

Added in: v1.10
  • ...pathSegments <Array<string>> 要附加到结果路径末尾的路径段。#
  • returns: <string>#

返回 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.setTimeout(timeout)

Added in: v1.10
  • timeout <number> 超时时间(以毫秒为单位)。#
  • returns: <void>#

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

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

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

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

testInfo.skip()

Added in: v1.10

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

testInfo.skip(condition[, description])

Added in: v1.10
  • condition <boolean> 跳过条件。当条件为 true 时跳过测试。#
  • description? <string> 可选的描述,将反映在测试报告中。#
  • returns: <void>#

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

testInfo.slow()

Added in: v1.10

将当前运行的测试标记为 "slow"(缓慢),将其超时时间设置为默认值的三倍。这类似于 test.slow()

testInfo.slow(condition[, description])

Added in: v1.10
  • condition <boolean> 当条件为 true 时,测试被标记为 "slow"。#
  • description? <string> 可选的描述,将反映在测试报告中。#
  • returns: <void>#

有条件地将当前运行的测试标记为 "slow",并带有可选描述,将其超时时间设置为默认值的三倍。这类似于 test.slow(condition[, description])

testInfo.snapshotPath(...pathSegments)

Added in: v1.10
  • ...pathSegments <Array<string>> 快照的名称或用于定义快照文件路径的路径段。同一测试文件中具有相同名称的快照应相同。#
  • returns: <string>#

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

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

testInfo.annotations

Added in: v1.10

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

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

testInfo.attachments

Added in: v1.10
  • type: <Array<Object>>
    • name <string> 附件名称。
    • contentType <string> 此附件的内容类型,以便在报告中正确显示,例如 'application/json''image/png'
    • path? <string> 附件文件在文件系统上的可选路径。
    • body? <Buffer> 可选的附件内容,用于代替文件。

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

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

testInfo.column

Added in: v1.10

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

testInfo.config

Added in: v1.10

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

testInfo.duration

Added in: v1.10

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

testInfo.error

Added in: v1.10

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

testInfo.errors

Added in: v1.10

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

testInfo.expectedStatus

Added in: v1.10
  • type: <"passed"|"failed"|"timedOut"|"skipped"|"interrupted">

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

Added in: v1.10

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

testInfo.fn

Added in: v1.10

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

testInfo.line

Added in: v1.10

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

testInfo.outputDir

Added in: v1.10

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

testInfo.parallelIndex

Added in: v1.10

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

也可以通过 process.env.TEST_PARALLEL_INDEX 获得。了解更多关于 Playwright Test 的 并行化和分片 的信息。

testInfo.project

Added in: v1.10

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

testInfo.repeatEachIndex

Added in: v1.10

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

testInfo.retry

Added in: v1.10

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

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();
// ...
});

testInfo.snapshotDir

Added in: v1.10

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

testInfo.snapshotSuffix

Added in: v1.10

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

testInfo.status

Added in: v1.10
  • type: <"passed"|"failed"|"timedOut"|"skipped"|"interrupted">

当前运行测试的实际状态。在测试完成后,可在 test.afterEach(hookFunction) 钩子和 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.stderr

Added in: v1.10

测试执行期间写入 process.stderrconsole.error 的输出。

testInfo.stdout

Added in: v1.10

测试执行期间写入 process.stdoutconsole.log 的输出。

testInfo.timeout

Added in: v1.10

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

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

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

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

testInfo.title

Added in: v1.10

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

testInfo.titlePath

Added in: v1.10

从项目开始的完整标题路径。

testInfo.workerIndex

Added in: v1.10

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

也可以通过 process.env.TEST_WORKER_INDEX 获得。了解更多关于 Playwright Test 的 并行化和分片 的信息。