Skip to main content




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.

  • 将测试标记为失败。Playwright Test 将运行此测试并确保它确实失败。如果测试没有失败,Playwright 测试将会诉说。 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.


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.


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

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

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


test('user profile', async ({ page }) => {{
type: 'issue',
description: '<some-issue>',
// ...