逾時
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
掛勾之間共享。
相同的逾時值也適用於 beforeAll
和 afterAll
掛勾,但它們不與任何測試共享時間。
在組態設定中設定測試逾時
import { defineConfig } from '@playwright/test';
export default defineConfig({
timeout: 120_000,
});
API 參考:testConfig.timeout。
為單一測試設定逾時
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
掛勾變更逾時
import { test, expect } from '@playwright/test';
test.beforeEach(async ({ page }, testInfo) => {
// 為執行此掛勾的所有測試延長 30 秒的逾時。
testInfo.setTimeout(testInfo.timeout + 30_000);
});
API 參考:testInfo.setTimeout()。
變更 beforeAll
/afterAll
掛勾的逾時
beforeAll
和 afterAll
掛勾有單獨的逾時,預設等於測試逾時。您可以透過在掛勾內呼叫 testInfo.setTimeout() 為每個掛勾單獨變更它。
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 逾時
import { defineConfig } from '@playwright/test';
export default defineConfig({
expect: {
timeout: 10_000,
},
});
API 參考:testConfig.expect。
為單一斷言指定 expect 逾時
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
您可以在組態設定中設定全域逾時。
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 } |
在組態設定中設定動作和導航逾時
import { defineConfig } from '@playwright/test';
export default defineConfig({
use: {
actionTimeout: 10 * 1000,
navigationTimeout: 30 * 1000,
},
});
API 參考:testOptions.actionTimeout 和 testOptions.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 });
});
佈置逾時
預設情況下,佈置 與測試共享逾時。然而,對於慢速佈置,特別是工作程序範圍的佈置,有單獨的逾時是很方便的。這樣您可以保持整體測試逾時較小,並給慢速佈置更多時間。
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()。