跳至主要内容

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

方法

附加

Added in: v1.51 testStepInfo.attach

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

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

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

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

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

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 (可选)

    • body string | Buffer (可选)#

      附件正文。与path互斥。

    • contentType string (可选)#

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

    • path string (可选)#

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

返回


skip()

Added in: v1.51 testStepInfo.skip()

中止当前运行的步骤并将其标记为跳过。适用于当前失败并计划近期修复的步骤。

用法

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)

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

用法

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 (可选)#

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