跳到主要内容

注解

引言

Playwright 支持在测试报告中显示的标签和注解。

你可以在任何时候添加自己的标签和注解,但 Playwright 内置了一些预设的注解:

  • test.skip() 将测试标记为不相关。Playwright 不会运行此类测试。当测试在某些配置中不适用时,使用此注解。
  • test.fail() 将测试标记为预期失败。Playwright 将运行此测试并确保它确实失败。如果测试没有失败,Playwright 将报错。
  • test.fixme() 将测试标记为需要修复。Playwright 不会运行此测试,这与 fail 注解不同。当运行测试很慢或导致崩溃时,使用 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,然后在测试报告中按标签过滤。或者你可能只想运行带有特定标签的测试。

要给测试打标签,可以在声明测试时提供额外的 details 对象,或者在测试标题中添加 @ 标记。请注意,标签必须以 @ 符号开头。

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 命令行选项运行带有特定标签的测试。

npx playwright test --grep @fast

或者如果你想要相反的效果,可以跳过带有特定标签的测试

npx playwright test --grep-invert @fast

要运行包含任一标签的测试(逻辑或操作符)

npx playwright test --grep "@fast|@slow"

或者使用正则表达式先行断言运行包含两个标签的测试(逻辑与操作符)

npx playwright test --grep "(?=.*@fast)(?=.*@slow)"

你也可以通过 testConfig.greptestProject.grep 在配置文件中过滤测试。

注解测试

如果你想用比标签更丰富的内容来注解你的测试,可以在声明测试时这样做。注解包含一个 type 和一个 description,以提供更多上下文,并且在 reporter API 中可用。Playwright 的内置 HTML 报告器会显示所有注解,但 type_ 符号开头的除外。

例如,用一个问题 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 中运行一组测试。

example.spec.ts

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 钩子,你可以将注解放在钩子本身中。

example.spec.ts

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 中。

example.spec.ts

test('example test', async ({ page, browser }) => {
test.info().annotations.push({
type: 'browser version',
description: browser.version(),
});

// ...
});