注解
简介
Playwright 支持在测试报告中显示的标签和注解。
您可以随时添加自己的标签和注释,但Playwright内置了一些标签:
- test.skip() 将测试标记为不相关。Playwright不会运行此类测试。当测试在某些配置中不适用时,请使用此注解。
- test.fail() 将测试标记为失败。Playwright会运行此测试并确保它确实失败。如果测试没有失败,Playwright会报错。
- test.fixme() 将测试标记为失败。与
fail
注解不同,Playwright不会运行此测试。当运行测试较慢或会崩溃时,使用fixme
。 - test.slow() 将测试标记为慢速测试,并将测试超时时间延长三倍。
可以为单个测试或一组测试添加注解。
内置注解可以是条件性的,在这种情况下,当条件为真时它们会生效,并且可能依赖于测试夹具。同一个测试上可以有多个注解,可能在不同的配置中。
聚焦一个测试
您可以聚焦某些测试。当存在聚焦测试时,仅运行这些测试。
test.only('focus this test', async ({ page }) => {
// Run only focused tests in the entire project.
});
跳过测试
将测试标记为跳过。
test.skip('skip this test', async ({ page }) => {
// This test is not run
});
有条件地跳过测试
您可以根据条件跳过某些测试。
test('skip this test', async ({ page, browserName }) => {
test.skip(browserName === 'firefox', 'Still working on it');
});
分组测试
你可以将测试分组,为它们赋予一个逻辑名称,或者将前置/后置钩子限定在组范围内。
import { test, expect } from '@playwright/test';
test.describe('two tests', () => {
test('one', async ({ page }) => {
// ...
});
test('two', async ({ page }) => {
// ...
});
});
标签测试
有时您可能想将测试标记为@fast
或@slow
,然后在测试报告中按标签进行筛选。或者您可能只想运行具有特定标签的测试。
要为测试添加标签,可以在声明测试时提供一个额外的详细信息对象,或者在测试标题中添加@
标记。请注意,标签必须以@
符号开头。
import { test, expect } from '@playwright/test';
test('test login page', {
tag: '@fast',
}, async ({ page }) => {
// ...
});
test('test full report @slow', async ({ page }) => {
// ...
});
您也可以为组中的所有测试添加标签或提供多个标签:
import { test, expect } from '@playwright/test';
test.describe('group', {
tag: '@report',
}, () => {
test('test report header', async ({ page }) => {
// ...
});
test('test full report', {
tag: ['@slow', '@vrt'],
}, async ({ page }) => {
// ...
});
});
您现在可以通过--grep
命令行选项运行具有特定标签的测试。
- Bash
- PowerShell
- 批量处理
npx playwright test --grep @fast
npx playwright test --grep "@fast"
npx playwright test --grep @fast
或者,如果您想要相反的效果,可以跳过带有特定标签的测试:
- Bash
- PowerShell
- 批量处理
npx playwright test --grep-invert @fast
npx playwright test --grep-invert "@fast"
npx playwright test --grep-invert @fast
要运行包含任一标签的测试(逻辑OR
运算符):
- Bash
- PowerShell
- 批量处理
npx playwright test --grep "@fast|@slow"
npx playwright test --grep --% "@fast^|@slow"
npx playwright test --grep "@fast^|@slow"
或者运行包含两个标签的测试(逻辑AND
运算符)使用正则表达式先行断言:
npx playwright test --grep "(?=.*@fast)(?=.*@slow)"
你也可以在配置文件中通过testConfig.grep和testProject.grep来过滤测试。
为测试添加注释
If you would like to annotate your tests with something more substantial than a tag, you can do that when declaring a test. Annotations have a type
and a description
for more context and available in reporter API. Playwright's built-in HTML reporter shows all annotations, except those where type
starts with _
symbol.
例如,要为测试添加问题URL的注释:
import { test, expect } from '@playwright/test';
test('test login page', {
annotation: {
type: 'issue',
description: 'https://github.com/microsoft/playwright/issues/23180',
},
}, async ({ page }) => {
// ...
});
您还可以为组中的所有测试添加注释或提供多个注释:
import { test, expect } from '@playwright/test';
test.describe('report tests', {
annotation: { type: 'category', description: 'report' },
}, () => {
test('test report header', async ({ page }) => {
// ...
});
test('test full report', {
annotation: [
{ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/23180' },
{ type: 'performance', description: 'very slow test!' },
],
}, async ({ page }) => {
// ...
});
});
有条件地跳过一组测试
例如,您可以通过传递回调函数来专门在Chromium中运行一组测试。
test.describe('chromium only', () => {
test.skip(({ browserName }) => browserName !== 'chromium', 'Chromium only!');
test.beforeAll(async () => {
// This hook is only run in Chromium.
});
test('test 1', async ({ page }) => {
// This test is only run in Chromium.
});
test('test 2', async ({ page }) => {
// This test is only run in Chromium.
});
});
在beforeEach
钩子中使用fixme
为了避免运行beforeEach
钩子,你可以在钩子本身中添加注解。
test.beforeEach(async ({ page, isMobile }) => {
test.fixme(isMobile, 'Settings page does not work in mobile yet');
await page.goto('http://localhost:3000/settings');
});
test('user profile', async ({ page }) => {
await page.getByText('My Profile').click();
// ...
});
运行时注解
在测试已经在运行时,你可以向test.info().annotations
添加注释。
test('example test', async ({ page, browser }) => {
test.info().annotations.push({
type: 'browser version',
description: browser.version(),
});
// ...
});