Câu hỏi phỏng vấn #13: Parallel execution trong Playwright hoạt động ra sao?
Trả lời mẫu:
Playwright Test hỗ trợ parallel execution mạnh mẽ:
1. Config workers:
// playwright.config.js
export default { workers: 4, // Số parallel workers fullyParallel: true, // Run all tests in parallel
}
2. Control parallel mode:
// Parallel by default trong file
test.describe.parallel('suite', () => { test('test 1', async ({ page }) => { // runs in parallel }); test('test 2', async ({ page }) => { // runs in parallel });
}); // Serial execution
test.describe.serial('dependent tests', () => { test('create user', async ({ page }) => { // runs first }); test('update user', async ({ page }) => { // runs after create });
});
3. Sharding cho CI:
#Split tests across machines
npx playwright test --shard=1/3
npx playwright test --shard=2/3
npx playwright test --shard=3/3
4. Worker-scoped fixtures:
// Shared per worker
test.use({ workerStorageState: async ({}, use, workerInfo) => { const id = workerInfo.workerIndex; await use(`worker-${id}-auth.json`); }
});
💡 Tips:
- Default: 1 worker per test file
- Tăng workers = tăng tốc độ nhưng tốn resources
- Use serial mode cho dependent tests
- Isolated browser contexts ensure test independence
Lời Kết
Playwright đang trở thành một trong những automation frameworks phổ biến nhất cho web testing. Thông qua series này, hy vọng bạn sẽ:
- Nắm vững kiến thức từ cơ bản đến nâng cao
- Tự tin trong các buổi phỏng vấn
- Áp dụng hiệu quả vào dự án thực tế
- Trở thành một phần của cộng đồng Playwright Việt Nam năng động
📚 Bắt đầu hành trình của bạn với: Bài 1: Playwright vs Selenium
💬 Có câu hỏi? Tham gia group Facebook của chúng mình!
⭐ Theo dõi series để không bỏ lỡ bài viết mới!