注解
介绍
Playwright 支持标签和注解,这些标签和注解会显示在测试报告中。
您可以随时添加自己的标签和注解,但 Playwright 附带了一些内置标签和注解
- test.skip() 将测试标记为不相关。Playwright 不会运行此类测试。当测试在某些配置中不适用时,请使用此注解。
- test.fail() 将测试标记为失败。Playwright 将运行此测试并确保它确实失败。如果测试没有失败,Playwright 将会报错。
- test.fixme() 将测试标记为失败。与
fail
注解相反,Playwright 不会运行此测试。当运行测试速度慢或崩溃时,请使用fixme
。 - test.slow() 将测试标记为慢速测试,并将测试超时时间增加三倍。
注解可以添加到单个测试或一组测试中。
内置注解可以是条件性的,在这种情况下,当条件为真时应用,并且可能依赖于测试 fixtures。同一个测试上可能存在多个注解,可能在不同的配置中。
聚焦一个测试
您可以聚焦某些测试。当存在聚焦测试时,仅运行这些测试。
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');
});
分组测试
您可以对测试进行分组,以便为它们提供逻辑名称或将 before/after 钩子限定到该组。
import { test, expect } from '@playwright/test';
test.describe('two tests', () => {
test('one', async ({ page }) => {
// ...
});
test('two', async ({ page }) => {
// ...
});
});
标记测试
有时您想将测试标记为 @fast
或 @slow
,然后在测试报告中按标签进行过滤。或者您可能只想运行具有特定标签的测试。
要标记测试,可以在声明测试时提供额外的详细信息对象,或者在测试标题中添加 @
-token。请注意,标签必须以 @
符号开头。
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
- Batch
npx playwright test --grep @fast
npx playwright test --grep "@fast"
npx playwright test --grep @fast
或者,如果您想要相反的效果,您可以跳过具有特定标签的测试
- Bash
- PowerShell
- Batch
npx playwright test --grep-invert @fast
npx playwright test --grep-invert "@fast"
npx playwright test --grep-invert @fast
运行包含任一标签的测试(逻辑 OR 运算符)
- Bash
- PowerShell
- Batch
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 过滤测试。
注解测试
如果您想用比标签更实质性的内容来注解您的测试,您可以在声明测试时这样做。注解具有 type
和 description
,以便提供更多上下文,并在报告器 API 中可用。Playwright 的内置 HTML 报告器显示所有注解,但 type
以 _
符号开头的注解除外。
例如,要使用 issue 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('https://127.0.0.1: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(),
});
// ...
});