Học Playwright tiếng Việt, Cộng đồng Playwright cho người Việt

Vọc Vạch Playwright

[Vọc Playwright] Timeout – phần 1

https://playwright.dev/docs/test-timeouts

Giới thiệu

  • Playwright có nhiều kiểu timeout cho các config đa dạng khác nhau
Timeout Giá trị mặc định Mô tả
Test timeout 30000 ms (30 giây) Test timeout cho mỗi test, bao gồm test, hooks và fixtures. Cấu hình trong file config: config = { timeout: 60000 }. Ghi đè: test.setTimeout(120000)
Expect timeout 5000 ms (5 giây) Timeout cho mỗi hành động assertion. Cấu hình trong file config: config = { expect: { timeout: 10000 } }. Để ghi đè: expect(locator).toBeVisible({ timeout: 10000 })`

Test timeout

  • Playwright bắt buộc mỗi test phải có timeout. Mặc định sẽ là 30 giây.
  • Tổng thời gian timeout sẽ bao gồm: test function, fixture, beforeEach và afterEach hook.
  • Khi timeout thì output ra trông sẽ như sau:
example.spec.ts:3:1 › basic test ===========================

Timeout of 30000ms exceeded.
  • beforeAllafterAll cũng sử dụng giá trị timeout này. Nghĩa là mỗi beforeAllafterAll đều có giá trị bằng với test timeout.
    • Ví dụ sau vẫn hợp lệ:
      • Set timeout = 10s
      • beforeAll chạy mất 9s
      • test chạy mất 5s
      • afterAll chạy mất 8s
    • Giải thích:
      • settimeout = 10s, nên beforeAll chạy 9s vẫn hợp lệ
      • Test chạy mất 5s < timeout => hợp lệ
      • afterAll chạy mất 8s < timeout => hợp lệ

Cài đặt test timeout ở file config

import { defineConfig } from '@playwright/test';

export default defineConfig({
  timeout: 5 * 60 * 1000,
});
  • Dùng cú pháp (5 * 60 * 1000) khá hay, giúp bạn biết được: đây là 5h.

Cài đặt timeout cho một test

  • Có 2 cách:
    • Gọi hàm test.slow() để nhân ba thời gian timeout của test lên.
    • Gọi hàm test.setTimeout(<time>) để đặt thời gian timeout mong muốn.
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);
  // ...
});

Thay đổi test timeout sử dụng testhook

  • Trường hợp bạn muốn extend tất cả các test lên một khoảng thời gian, bạn có thể dùng 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);
});

Thay đổi timeout của beforeAll/afterAll hook

  • beforeAll/ afterAll có timeout riêng. Mặc định thì giá trị này bằng với test timeout.
  • Bạn có thể thay đổi giá trị theo mong muốn
import { test, expect } from '@playwright/test';

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

Expect timeout

  • Mặc định thì assertion (expect) có timeout mặc định là 5s.
  • Assertion time không liên quan đến test timeout.
  • Nếu assertion timeout xảy ra thì lỗi trông sẽ như thế này:
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')"

Đặt giá trị expect trong config

import { defineConfig } from '@playwright/test';

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

Global timeout

  • Playwright support việc setting timeout cho toàn bộ tổng thời gian chạy test.
  • Việc này sẽ giúp kiểm soát resource tốt hơn.
  • Mặc định thì không có global timeout.
  • Bạn có thể set trong config như này:
// playwright.config.ts
import { defineConfig } from '@playwright/test';

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

Trả lời