Skip to main content

TestStepInfo

TestStepInfo 包含有关当前运行的测试步骤的信息。它作为参数传递给步骤函数。TestStepInfo 提供用于控制测试步骤执行的工具。

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

test('basic test', async ({ page, browserName }) => {
await test.step('check some behavior', async step => {
step.skip(browserName === 'webkit', 'The feature is not available in WebKit');
// ... rest of the step code
});
});

方法

🌐 Methods

attach

Added in: v1.51 testStepInfo.attach

将一个值或磁盘上的文件附加到当前测试步骤。一些报告工具会显示测试步骤的附件。必须指定 pathbody 中的一个,但不能同时指定两者。调用此方法会将附件归属到步骤,而不是 testInfo.attach(),后者会将所有附件存储在测试级别。

🌐 Attach a value or a file from disk to the current test step. Some reporters show test step attachments. Either path or body must be specified, but not both. Calling this method will attribute the attachment to the step, as opposed to testInfo.attach() which stores all attachments at the test level.

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

🌐 For example, you can attach a screenshot to the test step:

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

test('basic test', async ({ page }) => {
await page.goto('https://playwright.nodejs.cn');
await test.step('check page rendering', async step => {
const screenshot = await page.screenshot();
await step.attach('screenshot', { body: screenshot, contentType: 'image/png' });
});
});

或者你可以附加 API 返回的文件:

🌐 Or you can attach files returned by your APIs:

import { test, expect } from '@playwright/test';
import { download } from './my-custom-helpers';

test('basic test', async ({}) => {
await test.step('check download behavior', async step => {
const tmpPath = await download('a');
await step.attach('downloaded', { path: tmpPath });
});
});
note

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

用法

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

参数

  • name string#

    附件名称。名称也会被净化,并在保存到磁盘时用作文件名的前缀。

  • options Object (optional)

    • body string | Buffer (optional)#

      依附主体。与[路径](/api/class-teststepinfo.mdx#test-step-info-attach-option-path)互斥。

    • contentType string (optional)#

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

    • path string (optional)#

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

返回


skip()

Added in: v1.51 testStepInfo.skip()

中止当前正在运行的步骤,并将其标记为已跳过。对于当前失败且计划在短期内修复的步骤非常有用。

🌐 Abort the currently running step and mark it as skipped. Useful for steps that are currently failing and planned for a near-term fix.

用法

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

test('my test', async ({ page }) => {
await test.step('check expectations', async step => {
step.skip();
// step body below will not run
// ...
});
});

skip(condition)

Added in: v1.51 testStepInfo.skip(condition)

有条件地终止当前正在运行的步骤,并将其标记为跳过,可附加描述。适用于在某些情况下不应执行的步骤。

🌐 Conditionally abort the currently running step and mark it as skipped with an optional description. Useful for steps that should not be executed in some cases.

用法

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

test('my test', async ({ page, isMobile }) => {
await test.step('check desktop expectations', async step => {
step.skip(isMobile, 'not present in the mobile layout');
// step body below will not run
// ...
});
});

参数

  • condition boolean#

    跳过条件。当条件为 true 时,测试步骤会被跳过。

  • description string (optional)#

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


属性

🌐 Properties

titlePath

Added in: v1.55 testStepInfo.titlePath

从测试文件名称开始的完整标题路径,包括步骤标题。另请参见 testInfo.titlePath

🌐 The full title path starting with the test file name, including the step titles. See also testInfo.titlePath.

用法

testStepInfo.titlePath

类型

  • [数组]<[字符串]>