Skip to main content

注释

介绍

Introduction

Playwright Test 支持测试注释来处理失败、不稳定、跳过、焦点和标签测试:

Playwright Test supports test annotations to deal with failures, flakiness, skip, focus and tag tests:

  • test.skip() 将测试标记为不相关。Playwright 测试不运行这样的测试。当测试在某些配置中不适用时使用此注释。

    test.skip() marks the test as irrelevant. Playwright Test does not run such a test. Use this annotation when the test is not applicable in some configuration.

  • test.fail() 将测试标记为失败。Playwright Test 将运行此测试并确保它确实失败。如果测试没有失败,Playwright 测试将会诉说。

    test.fail() marks the test as failing. Playwright Test will run this test and ensure it does indeed fail. If the test does not fail, Playwright Test will complain.

  • test.fixme() 将测试标记为失败。与 fail 注释相反,Playwright 测试不会运行此测试。当运行测试缓慢或崩溃时使用 fixme

    test.fixme() marks the test as failing. Playwright Test will not run this test, as opposed to the fail annotation. Use fixme when running the test is slow or crashes.

  • test.slow() 将测试标记为慢速并将测试超时增加三倍。

    test.slow() marks the test as slow and triples the test timeout.

注释可以用于单个测试或一组测试。注释可以是有条件的,在这种情况下,它们在条件为真时应用。注释可能取决于测试装置。同一个测试可能有多个注释,可能处于不同的配置中。

Annotations can be used on a single test or a group of tests. Annotations can be conditional, in which case they apply when the condition is truthy. Annotations may depend on test fixtures. There could be multiple annotations on the same test, possibly in different configurations.

集中测试

Focus a test

你可以集中一些测试。当存在集中测试时,仅运行这些测试。

You can focus some tests. When there are focused tests, only these tests run.

test.only('focus this test', async ({ page }) => {
// Run only focused tests in the entire project.
});

跳过测试

Skip a test

将测试标记为已跳过。

Mark a test as skipped.

test.skip('skip this test', async ({ page }) => {
// This test is not run
});

有条件地跳过测试

Conditionally skip a test

你可以根据情况跳过某些测试。

You can skip certain test based on the condition.

test('skip this test', async ({ page, browserName }) => {
test.skip(browserName === 'firefox', 'Still working on it');
});

群组测试

Group tests

你可以对测试进行分组,为它们指定一个逻辑名称,或者在组的钩子之前/之后确定范围。

You can group tests to give them a logical name or to scope before/after hooks to the group.

import { test, expect } from '@playwright/test';

test.describe('two tests', () => {
test('one', async ({ page }) => {
// ...
});

test('two', async ({ page }) => {
// ...
});
});

标签测试

Tag tests

有时你想将测试标记为 @fast@slow,并且只运行具有特定标记的测试。我们建议你为此使用 --grep--grep-invert 命令行标志:

Sometimes you want to tag your tests as @fast or @slow and only run the tests that have the certain tag. We recommend that you use the --grep and --grep-invert command line flags for that:

import { test, expect } from '@playwright/test';

test('Test login page @fast', async ({ page }) => {
// ...
});

test('Test full report @slow', async ({ page }) => {
// ...
});

然后你将只能运行该测试:

You will then be able to run only that test:

npx playwright test --grep @fast

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

Or if you want the opposite, you can skip the tests with a certain tag:

npx playwright test --grep-invert @fast

要运行包含任一标签(逻辑 OR 运算符)的测试:

To run tests containing either tag (logical OR operator):

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

或者使用正则表达式先行运行包含两个标签(逻辑 AND 运算符)的测试:

Or run tests containing both tags (logical AND operator) using regex lookaheads:

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

有条件地跳过一组测试

Conditionally skip a group of tests

例如,你可以通过传递回调仅在 Chromium 中运行一组测试。

For example, you can run a group of tests just in Chromium by passing a callback.

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

Use fixme in beforeEach hook

为了避免运行 beforeEach 钩子,你可以在钩子本身中添加注释。

To avoid running beforeEach hooks, you can put annotations in the hook itself.

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();
// ...
});

自定义注释

Custom annotations

还可以以注释的形式将自定义元数据添加到测试中。注释是可通过 test.info().annotations 访问的键/值对。很多报告器都会给出注释,例如 'html'

It's also possible to add custom metadata in the form of annotations to your tests. Annotations are key/value pairs accessible via test.info().annotations. Many reporters show annotations, for example 'html'.

example.spec.ts

test('user profile', async ({ page }) => {
test.info().annotations.push({
type: 'issue',
description: 'https://github.com/microsoft/playwright/issues/<some-issue>',
});
// ...
});