逾時
Playwright Test 為各種任務提供多個可配置的逾時。
逾時 | 預設 | 描述 |
---|---|---|
測試逾時 | 30_000 毫秒 | 每個測試的逾時 於設定中設定 { timeout: 60_000 } 在測試中覆寫 test.setTimeout(120_000) |
斷言逾時 | 5_000 毫秒 | 每個斷言的逾時 於設定中設定 { expect: { timeout: 10_000 } } 在測試中覆寫 expect(locator).toBeVisible({ timeout: 10_000 }) |
測試逾時
Playwright Test 會對每個測試強制執行逾時,預設為 30 秒。 測試函式、夾具設定和 beforeEach
hook 所花費的時間都包含在測試逾時中。
逾時的測試會產生以下錯誤
example.spec.ts:3:1 › basic test ===========================
Timeout of 30000ms exceeded.
在測試函式完成後,fixture 拆解和 afterEach
hook 之間會共用另一個相同值的獨立逾時。
相同的逾時值也適用於 beforeAll
和 afterAll
hook,但它們不與任何測試共用時間。
在設定中設定測試逾時
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(); // Easy way to triple the default timeout
// ...
});
test('very slow test', async ({ page }) => {
test.setTimeout(120_000);
// ...
});
API 參考: test.setTimeout() 和 test.slow()。
從 beforeEach
hook 變更逾時
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 + 30_000);
});
API 參考: testInfo.setTimeout()。
變更 beforeAll
/afterAll
hook 的逾時
beforeAll
和 afterAll
hook 有個別的逾時,預設與測試逾時相同。 您可以針對每個 hook,透過在 hook 內呼叫 testInfo.setTimeout() 來個別變更。
import { test, expect } from '@playwright/test';
test.beforeAll(async () => {
// Set timeout for this hook.
test.setTimeout(60000);
});
API 參考: testInfo.setTimeout()。
斷言逾時
自動重試斷言 (例如 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')"
在設定中設定斷言逾時
import { defineConfig } from '@playwright/test';
export default defineConfig({
expect: {
timeout: 10_000,
},
});
API 參考: testConfig.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 毫秒 | hook 的逾時 在 hook 中設定 test.setTimeout(60_000) |
Fixture 逾時 | 無逾時 | 個別 fixture 的逾時 在 fixture 中設定 { 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.org.tw', { timeout: 30000 });
await page.getByText('Get Started').click({ timeout: 10000 });
});
Fixture 逾時
預設情況下,fixture 與測試共用逾時。 但是,對於速度較慢的 fixture,尤其是worker-scoped 的 fixture,擁有個別的逾時會更方便。 這樣一來,您可以保持整體測試逾時時間較短,並為速度較慢的 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: 60_000 }]
});
test('example test', async ({ slowFixture }) => {
// ...
});
API 參考: test.extend()。