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', '该功能在 WebKit 中不可用');
// ... 步骤代码的其余部分
});
});
方法
attach
添加于: v1.51将值或磁盘文件附加到当前测试步骤。某些报告器会显示测试步骤附件。必须指定 path 或 body 其中之一,但不能同时指定。调用此方法会将附件归属于当前步骤,与 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 });
});
});
:::注意 testStepInfo.attach() 会自动处理将附件文件复制到报告器可访问的位置。在等待 attach 调用完成后,您可以安全地删除附件。 :::
用法
await testStepInfo.attach(name);
await testStepInfo.attach(name, options);
参数
-
附件名称。名称会被清理并用作保存到磁盘时的文件名前缀。
-
options
Object (可选)
返回值
skip()
发布于: v1.51中止当前运行的步骤并将其标记为跳过。适用于当前失败但计划近期修复的步骤。
用法
import { test, expect } from '@playwright/test';
test('my test', async ({ page }) => {
await test.step('check expectations', async step => {
step.skip();
// 下面的步骤体将不会执行
// ...
});
});
skip(condition)
发布于: v1.51根据条件中止当前运行的步骤,并可选择性地添加描述信息将其标记为跳过。适用于某些情况下不应执行的步骤。
用法
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');
// 下面的步骤体将不会执行
// ...
});
});
参数