TestStepInfo
TestStepInfo
包含有关当前正在运行的测试步骤的信息。它作为参数传递给步骤函数。TestStepInfo
提供实用程序来控制测试步骤的执行。
¥TestStepInfo
contains information about currently running test step. It is passed as an argument to the step function. TestStepInfo
provides utilities to control test step execution.
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将磁盘中的值或文件附加到当前测试步骤。一些报告器显示测试步骤附件。必须指定 path 或 body,但不能同时指定两者。调用此方法将把附件归因于该步骤,而不是 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 });
});
});
testStepInfo.attach() 自动将附件复制到报告器可以访问的位置。等待附加调用后,你可以安全地删除附件。
¥testStepInfo.attach() automatically takes care of copying attached files to a location that is accessible to reporters. You can safely remove the attachment after awaiting the attach call.
用法
¥Usage
await testStepInfo.attach(name);
await testStepInfo.attach(name, options);
参数
¥Arguments
附件名称。保存到磁盘时,该名称也将被清理并用作文件名的前缀。
¥Attachment name. The name will also be sanitized and used as the prefix of file name when saving to disk.
附件体。与 path 互斥。
¥Attachment body. Mutually exclusive with path.
要在报告中正确显示的此附件的内容类型,例如 'application/json'
或 'image/png'
。如果省略,则根据 path 推断内容类型,或者对于 string 附件默认为 text/plain
,对于 缓冲 附件默认为 application/octet-stream
。
¥Content type of this attachment to properly present in the report, for example 'application/json'
or 'image/png'
. If omitted, content type is inferred based on the path, or defaults to text/plain
for string attachments and application/octet-stream
for Buffer attachments.
文件系统上附加文件的路径。与 body 互斥。
¥Path on the filesystem to the attached file. Mutually exclusive with body.
返回
¥Returns
skip()
Added in: v1.51中止当前正在运行的步骤并将其标记为已跳过。对于当前失败并计划在近期修复的步骤很有用。
¥Abort the currently running step and mark it as skipped. Useful for steps that are currently failing and planned for a near-term fix.
用法
¥Usage
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有条件地中止当前正在运行的步骤并将其标记为已跳过,并带有可选描述。对于在某些情况下不应执行的步骤很有用。
¥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.
用法
¥Usage
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
// ...
});
});
参数
¥Arguments
跳过条件。当条件为 true
时,将跳过测试步骤。
¥A skip condition. Test step is skipped when the condition is true
.
将反映在测试报告中的可选描述。
¥Optional description that will be reflected in a test report.