Skip to main content

測試組態設定

簡介

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://localhost: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://localhost: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 掛勾所花費的時間都包含在測試逾時中。

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.expect類似 expect(locator).toHaveText()Web 優先斷言預設有單獨的 5 秒逾時。這是 expect() 等待條件滿足的最長時間。了解更多關於測試與 expect 逾時以及如何為單一測試設定它們。
expect(page).toHaveScreenshot()expect(locator).toHaveScreenshot() 方法的組態設定。
expect(value).toMatchSnapshot()expect(locator).toMatchSnapshot() 方法的組態設定。