Skip to main content

逾時

Playwright Test 對各種任務具有多個可組態設定的逾時。

逾時預設值說明
測試逾時30_000 ms每個測試的逾時
在組態設定中設定
{ timeout: 60_000 }
在測試中覆寫
test.setTimeout(120_000)
Expect 逾時5_000 ms每個斷言的逾時
在組態設定中設定
{ expect: { timeout: 10_000 } }
在測試中覆寫
expect(locator).toBeVisible({ timeout: 10_000 })

測試逾時

Playwright Test 為每個測試強制執行逾時,預設為 30 秒。測試函數、佈置設定和 beforeEach 掛勾所花費的時間都包含在測試逾時中。

逾時的測試會產生以下錯誤:

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

Timeout of 30000ms exceeded.

額外的單獨逾時(具有相同值)在測試函數完成後,在佈置拆除和 afterEach 掛勾之間共享。

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

在組態設定中設定測試逾時

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

export default defineConfig({
timeout: 120_000,
});

API 參考:testConfig.timeout

為單一測試設定逾時

example.spec.ts
import { test, expect } from '@playwright/test';

test('slow test', async ({ page }) => {
test.slow(); // 將預設逾時增加三倍的簡便方法
// ...
});

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

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

beforeEach 掛勾變更逾時

example.spec.ts
import { test, expect } from '@playwright/test';

test.beforeEach(async ({ page }, testInfo) => {
// 為執行此掛勾的所有測試延長 30 秒的逾時。
testInfo.setTimeout(testInfo.timeout + 30_000);
});

API 參考:testInfo.setTimeout()

變更 beforeAll/afterAll 掛勾的逾時

beforeAllafterAll 掛勾有單獨的逾時,預設等於測試逾時。您可以透過在掛勾內呼叫 testInfo.setTimeout() 為每個掛勾單獨變更它。

example.spec.ts
import { test, expect } from '@playwright/test';

test.beforeAll(async () => {
// 為此掛勾設定逾時。
test.setTimeout(60000);
});

API 參考:testInfo.setTimeout()

Expect 逾時

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')"

在組態設定中設定 expect 逾時

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

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

API 參考:testConfig.expect

為單一斷言指定 expect 逾時

example.spec.ts
import { test, expect } from '@playwright/test';

test('example', async ({ page }) => {
await expect(locator).toHaveText('hello', { timeout: 10_000 });
});

全域逾時

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: 3_600_000,
});

API 參考:testConfig.globalTimeout

進階:低階逾時

這些是由測試執行器預先組態設定的低階逾時,您應該不需要變更這些。如果您因為測試不穩定而來到此區段,很可能您應該在其他地方尋找解決方案。

逾時預設值說明
動作逾時無逾時每個動作的逾時
在組態設定中設定
{ use: { actionTimeout: 10_000 } }
在測試中覆寫
locator.click({ timeout: 10_000 })
導航逾時無逾時每個導航動作的逾時
在組態設定中設定
{ use: { navigationTimeout: 30_000 } }
在測試中覆寫
page.goto('/', { timeout: 30_000 })
全域逾時無逾時整個測試執行的全域逾時
在組態設定中設定
{ globalTimeout: 3_600_000 }
beforeAll/afterAll 逾時30_000 ms掛勾的逾時
在掛勾中設定
test.setTimeout(60_000)
佈置逾時無逾時個別佈置的逾時
在佈置中設定
{ scope: 'test', timeout: 30_000 }

在組態設定中設定動作和導航逾時

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

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

API 參考:testOptions.actionTimeouttestOptions.navigationTimeout

為單一動作設定逾時

example.spec.ts
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 });
});

佈置逾時

預設情況下,佈置 與測試共享逾時。然而,對於慢速佈置,特別是工作程序範圍的佈置,有單獨的逾時是很方便的。這樣您可以保持整體測試逾時較小,並給慢速佈置更多時間。

example.spec.ts
import { test as base, expect } from '@playwright/test';

const test = base.extend<{ slowFixture: string }>({
slowFixture: [async ({}, use) => {
// ... 執行慢速操作 ...
await use('hello');
}, { timeout: 60_000 }]
});

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

API 參考:test.extend()