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
});
});
方法
attach
添加于: v1.51将一个值或磁盘上的文件附加到当前测试步骤。某些报告器会显示测试步骤的附件。必须指定 path 或 body 其中之一,但不能同时指定两者。调用此方法会将附件归因于该步骤,这与 testInfo.attach() 不同,后者将所有附件存储在测试级别。
例如,你可以将屏幕截图附加到测试步骤:
import { test, expect } from '@playwright/test';
test('basic test', async ({ page }) => {
await page.goto('https://playwright.net.cn');
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() 会自动处理将附件文件复制到报告器可访问的位置。在 await attach 调用后,你可以安全地删除该附件。
用法
await testStepInfo.attach(name);
await testStepInfo.attach(name, options);
参数
-
附件名称。保存到磁盘时,名称也会被清理并用作文件名的前缀。
-
optionsObject (可选)
返回
skip()
添加于: v1.51中止当前正在运行的步骤并将其标记为已跳过。对于当前失败且计划在短期内修复的步骤非常有用。
用法
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)
添加于: 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');
// step body below will not run
// ...
});
});
参数
属性
titlePath
添加于:v1.55从测试文件名开始的完整标题路径,包括步骤标题。另请参阅 testInfo.titlePath。
用法
testStepInfo.titlePath
类型