[Playwright Interview question #29]: Cách tăng test timeout trong Playwright?
Câu hỏi phỏng vấn #29: How to increase test timeout in Playwright?
Trả lời mẫu:
1. Global Timeout Configuration:
// playwright.config.js
export default {
timeout: 60000, // 60 seconds for all tests
expect: {
timeout: 10000 // 10 seconds for assertions
}
};
2. Per Test Timeout:
test('slow test', async ({ page }) => {
test.setTimeout(120000); // 2 minutes for this test
await page.goto('/slow-page');
});
// Or use slow modifier
test.slow('very slow test', async ({ page }) => {
// Automatically triples the timeout
});
3. Action Timeout:
// Specific action timeout
await page.click('#button', { timeout: 30000 });
// Navigation timeout
await page.goto('/', { timeout: 60000 });
// Wait timeout
await page.waitForSelector('.element', { timeout: 20000 });
4. Project-Level Timeout:
// playwright.config.js
projects: [
{
name: 'slow-tests',
timeout: 180000, // 3 minutes
},
{
name: 'fast-tests',
timeout: 30000, // 30 seconds
}
]
5. Hook Timeouts:
test.beforeEach(async ({ page }) => {
test.setTimeout(30000); // For this hook
await page.goto('/');
});
6. No Timeout:
// Disable timeout (use carefully!)
test.setTimeout(0);
// For specific action
await page.click('#button', { timeout: 0 });
7. Different Timeout Types:
export default {
// Test timeout
timeout: 30000,
// Global setup timeout
globalTimeout: 600000,
// Expect assertion timeout
expect: { timeout: 5000 },
// Action timeout
use: {
actionTimeout: 10000,
navigationTimeout: 30000
}
};
💡 Tips:
- Increase timeout cho slow APIs/pages
- Use test.slow() instead of hardcoding
- Set different timeouts cho different environments
- Monitor tests requiring high timeouts - có thể cần optimization
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!
All rights reserved