註解
簡介
Playwright 支援標籤和註解,這些標籤和註解會顯示在測試報告中。
您可以隨時新增自己的標籤和註解,但 Playwright 內建了一些。
- test.skip() 將測試標記為不相關。Playwright 不會執行此類測試。當測試在某些設定中不適用時,請使用此註解。
- test.fail() 將測試標記為失敗。Playwright 將執行此測試,並確保它確實失敗。如果測試沒有失敗,Playwright 會抱怨。
- test.fixme() 將測試標記為失敗。與
fail
註解相反,Playwright 不會執行此測試。當執行測試速度緩慢或崩潰時,請使用fixme
。 - test.slow() 將測試標記為緩慢,並將測試逾時時間增加三倍。
註解可以新增到單個測試或一組測試。
內建註解可以是條件式的,在這種情況下,它們會在條件為真時套用,並且可能取決於測試 fixture。同一個測試上可能有多個註解,可能在不同的設定中。
聚焦測試
您可以聚焦某些測試。當有聚焦的測試時,只會執行這些測試。
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 hook 的範圍限定為群組。
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
命令列選項執行具有特定標籤的測試。
- 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"
或使用 regex lookahead 執行包含兩個標籤的測試 (邏輯 AND
運算子)
npx playwright test --grep "(?=.*@fast)(?=.*@slow)"
您也可以透過 testConfig.grep 和 testProject.grep 在組態檔中篩選測試。
註解測試
如果您想要使用比標籤更實質的內容來註解測試,您可以在宣告測試時執行此操作。註解具有 type
和 description
,以提供更多上下文,並在報告器 API 中提供。Playwright 的內建 HTML 報告器會顯示所有註解,但 type
以 _
符號開頭的註解除外。
例如,若要使用問題網址註解測試
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
hook 中使用 fixme
若要避免執行 beforeEach
hook,您可以將註解放在 hook 本身中。
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(),
});
// ...
});