Skip to main content

註釋

簡介

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');
});

群組測試

您可以將測試分組以為它們提供邏輯名稱,或將 before/after 掛勾的範圍限定在群組內。

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 命令行選項執行具有特定標籤的測試。

npx playwright test --grep @fast

或者如果您想要相反的情況,您可以跳過具有特定標籤的測試:

npx playwright test --grep-invert @fast

要執行包含任一標籤的測試(邏輯 OR 運算子):

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

或使用正規表示式先行斷言執行包含兩個標籤的測試(邏輯 AND 運算子):

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

您也可以透過 testConfig.greptestProject.grep 在組態檔案中篩選測試。

標註測試

如果您想用比標籤更實質的東西來標註您的測試,您可以在宣告測試時這樣做。註釋具有 typedescription 以提供更多內容,並在報告器 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(),
});

// ...
});