Skip to main content

超時

簡介

Playwright Test 有多個可配置的超時設定,用於各種任務。

TimeoutDefaultDescription
Test timeout30000 ms每個測試的超時時間,包括測試、hooks 和 fixtures:
Set default
config = { timeout: 60000 }
Override
test.setTimeout(120000)
Expect timeout5000 ms每個斷言的超時時間:
Set default
config = { expect: { timeout: 10000 } }
Override
expect(locator).toBeVisible({ timeout: 10000 })

測試超時

Playwright Test 對每個測試強制執行超時,預設為 30 秒。測試函式、fixtures、beforeEachafterEach hooks 所花費的時間包含在測試超時內。

測試超時會產生以下錯誤:

example.spec.ts:3:1 › basic test ===========================

Timeout of 30000ms exceeded.

相同的超時值也適用於 beforeAllafterAll 鉤子,但它們不與任何測試共享時間。

在 config 中設定測試超時時間

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

export default defineConfig({
timeout: 5 * 60 * 1000,
});

API 參考: testConfig.timeout

為單個測試設定 timeout

import { test, expect } from '@playwright/test';

test('slow test', async ({ page }) => {
test.slow(); // Easy way to triple the default timeout
// ...
});

test('very slow test', async ({ page }) => {
test.setTimeout(120000);
// ...
});

API 參考: test.setTimeout()test.slow()

beforeEach 鉤子更改超時設定

import { test, expect } from '@playwright/test';

test.beforeEach(async ({ page }, testInfo) => {
// Extend timeout for all tests running this hook by 30 seconds.
testInfo.setTimeout(testInfo.timeout + 30000);
});

API reference: testInfo.setTimeout

更改 beforeAll/afterAll 鉤子的超時時間

beforeAllafterAll 鉤子有一個單獨的超時設定,預設等於測試超時。你可以在鉤子內呼叫 testInfo.setTimeout() 來分別更改每個鉤子的超時設定。

import { test, expect } from '@playwright/test';

test.beforeAll(async () => {
// Set timeout for this hook.
test.setTimeout(60000);
});

API reference: testInfo.setTimeout

預期超時

Web-first 斷言如 expect(locator).toHaveText() 有一個單獨的超時時間,預設為 5 秒。斷言超時與測試超時無關。它會產生以下錯誤:

example.spec.ts:3:1 › basic test ===========================

Error: expect(received).toHaveText(expected)

Expected string: "my text"
Received string: ""
Call log:
- expect.toHaveText with timeout 5000ms
- waiting for "locator('button')"

在 config 中設定 expect timeout

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

export default defineConfig({
expect: {
timeout: 10 * 1000,
},
});

全域 timeout

Playwright Test 支援整個測試執行的超時設定。這可以防止在所有事情出錯時過度使用資源。沒有預設的全域超時,但你可以在配置中設定一個合理的時間,例如一小時。全域超時會產生以下錯誤:

Running 1000 tests using 10 workers

514 skipped
486 passed
Timed out waiting 3600s for the entire test run

你可以在配置中設定全域超時。

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

export default defineConfig({
globalTimeout: 60 * 60 * 1000,
});

API 參考: testConfig.globalTimeout

進階: 低層級超時

這些是由測試執行器預先配置的低層級超時設定,你不需要更改這些。如果你因為測試不穩定而來到這個部分,很可能你應該在其他地方尋找解決方案。

TimeoutDefaultDescription
Action timeoutno timeout每個動作的超時時間:
Set default
config = { use: { actionTimeout: 10000 } }
Override
locator.click({ timeout: 10000 })
Navigation timeoutno timeout每個導航動作的超時時間:
Set default
config = { use: { navigationTimeout: 30000 } }
Override
page.goto('/', { timeout: 30000 })
Global timeoutno timeout整個測試執行的全域超時時間:
Set in config
config = { globalTimeout: 60*60*1000 }
beforeAll/afterAll timeout30000 ms鉤子的超時時間:
Set in hook
test.setTimeout(60000)
Fixture timeoutno timeout單個夾具的超時時間:
Set in fixture
{ scope: 'test', timeout: 30000 }

設定單一斷言的超時時間

import { test, expect } from '@playwright/test';

test('basic test', async ({ page }) => {
await expect(page.getByRole('button')).toHaveText('Sign in', { timeout: 10000 });
});

在配置中設定操作和導航超時時間

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

export default defineConfig({
use: {
actionTimeout: 10 * 1000,
navigationTimeout: 30 * 1000,
},
});

API 參考: testOptions.actionTimeouttestOptions.navigationTimeout

設定單個動作的超時時間

import { test, expect } from '@playwright/test';

test('basic test', async ({ page }) => {
await page.goto('https://playwright.dev', { timeout: 30000 });
await page.getByText('Get Started').click({ timeout: 10000 });
});

固定裝置超時

預設情況下,fixture 與測試共享超時時間。然而,對於慢速的 fixtures,特別是 worker-scoped 的那些,擁有單獨的超時時間會更方便。這樣你可以保持整體測試的超時時間較短,並給慢速的 fixture 更多時間。

import { test as base, expect } from '@playwright/test';

const test = base.extend<{ slowFixture: string }>({
slowFixture: [async ({}, use) => {
// ... perform a slow operation ...
await use('hello');
}, { timeout: 60000 }]
});

test('example test', async ({ slowFixture }) => {
// ...
});

API 參考: test.extend()