跳到主要內容

測試配置

簡介

Playwright 提供了許多選項來配置測試的運行方式。您可以在配置文件中指定這些選項。請注意,測試運行器選項是頂層的,不要將它們放在 use 區段中。

基本配置

以下是一些最常見的配置選項。

import { defineConfig, devices } from '@playwright/test';

export default defineConfig({
// Look for test files in the "tests" directory, relative to this configuration file.
testDir: 'tests',

// Run all tests in parallel.
fullyParallel: true,

// Fail the build on CI if you accidentally left test.only in the source code.
forbidOnly: !!process.env.CI,

// Retry on CI only.
retries: process.env.CI ? 2 : 0,

// Opt out of parallel tests on CI.
workers: process.env.CI ? 1 : undefined,

// Reporter to use
reporter: 'html',

use: {
// Base URL to use in actions like `await page.goto('/')`.
baseURL: 'http://127.0.0.1:3000',

// Collect trace when retrying the failed test.
trace: 'on-first-retry',
},
// Configure projects for major browsers.
projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
},
],
// Run your local dev server before starting the tests.
webServer: {
command: 'npm run start',
url: 'http://127.0.0.1:3000',
reuseExistingServer: !process.env.CI,
},
});
選項描述
testConfig.forbidOnly如果任何測試被標記為 test.only,是否以錯誤退出。在 CI 上很有用。
testConfig.fullyParallel讓所有檔案中的所有測試並行運行。有關更多詳細信息,請參閱平行處理分片
testConfig.projects在多個配置或多個瀏覽器中運行測試
testConfig.reporter要使用的報告器。請參閱測試報告器以了解更多關於哪些報告器可用。
testConfig.retries每個測試的最大重試次數。請參閱測試重試以了解更多關於重試。
testConfig.testDir包含測試檔案的目錄。
testConfig.use帶有 use{} 的選項
testConfig.webServer要在測試期間啟動伺服器,請使用 webServer 選項
testConfig.workers用於並行測試的最大並行工作進程數。也可以設置為邏輯 CPU 核心的百分比,例如 '50%'。有關更多詳細信息,請參閱平行處理分片

過濾測試

通過 glob 模式或正則表達式過濾測試。

playwright.config.ts
import { defineConfig } from '@playwright/test';

export default defineConfig({
// Glob patterns or regular expressions to ignore test files.
testIgnore: '*test-assets',

// Glob patterns or regular expressions that match test files.
testMatch: '*todo-tests/*.spec.ts',
});
選項描述
testConfig.testIgnore在查找測試檔案時應忽略的 glob 模式或正則表達式。例如,'*test-assets'
testConfig.testMatch匹配測試檔案的 glob 模式或正則表達式。例如,'*todo-tests/*.spec.ts'。默認情況下,Playwright 運行 .*(test|spec).(js|ts|mjs) 檔案。

進階配置

playwright.config.ts
import { defineConfig } from '@playwright/test';

export default defineConfig({
// Folder for test artifacts such as screenshots, videos, traces, etc.
outputDir: 'test-results',

// path to the global setup files.
globalSetup: require.resolve('./global-setup'),

// path to the global teardown files.
globalTeardown: require.resolve('./global-teardown'),

// Each test is given 30 seconds.
timeout: 30000,

});
選項描述
testConfig.globalSetup全域設定檔案的路徑。此檔案將在所有測試之前被要求並運行。它必須導出一個單一函數。
testConfig.globalTeardown全域拆解檔案的路徑。此檔案將在所有測試之後被要求並運行。它必須導出一個單一函數。
testConfig.outputDir用於測試產出物(如螢幕截圖、影片、追蹤等)的資料夾。
testConfig.timeoutPlaywright 對每個測試強制執行逾時,默認為 30 秒。測試函數、測試夾具和 beforeEach hooks 花費的時間都包含在測試逾時中。

Expect 選項

expect 斷言函式庫的配置。

playwright.config.ts
import { defineConfig } from '@playwright/test';

export default defineConfig({
expect: {
// Maximum time expect() should wait for the condition to be met.
timeout: 5000,

toHaveScreenshot: {
// An acceptable amount of pixels that could be different, unset by default.
maxDiffPixels: 10,
},

toMatchSnapshot: {
// An acceptable ratio of pixels that are different to the
// total amount of pixels, between 0 and 1.
maxDiffPixelRatio: 0.1,
},
},

});
選項描述
testConfig.expectWeb first 斷言,例如 expect(locator).toHaveText(),默認情況下有 5 秒的單獨逾時。這是 expect() 應等待條件滿足的最長時間。了解更多關於測試和 expect 逾時以及如何為單個測試設置它們。
expect(page).toHaveScreenshot()expect(locator).toHaveScreenshot() 方法的配置。
expect(value).toMatchSnapshot()expect(locator).toMatchSnapshot() 方法的配置。