[Playwright Interview question #27]: Cách xử lý browser permissions trong Playwright?
Câu hỏi phỏng vấn #27: How do you handle browser permissions in Playwright?
Trả lời mẫu:
1. Grant Permissions:
// Grant specific permissions
const context = await browser.newContext({
permissions: ['geolocation', 'camera', 'microphone']
});
// Grant for specific origin
await context.grantPermissions(['notifications'], {
origin: 'https://example.com'
});
2. Geolocation Permission:
const context = await browser.newContext({
permissions: ['geolocation'],
geolocation: { latitude: 59.95, longitude: 30.31667 }
});
// Update location
await context.setGeolocation({ latitude: 40.7128, longitude: -74.0060 });
3. Camera/Microphone:
const context = await browser.newContext({
permissions: ['camera', 'microphone']
});
// Test video call features
await page.goto('/video-call');
await page.click('#start-call');
4. Notifications:
// Grant notification permission
await context.grantPermissions(['notifications']);
// Listen for notifications
page.on('dialog', async dialog => {
console.log('Notification:', dialog.message());
await dialog.accept();
});
5. Clipboard Access:
const context = await browser.newContext({
permissions: ['clipboard-read', 'clipboard-write']
});
// Test clipboard
await page.evaluate(() => {
navigator.clipboard.writeText('Hello Playwright');
});
6. Clear/Deny Permissions:
// Clear all permissions
await context.clearPermissions();
// Deny by not granting
const context = await browser.newContext({
permissions: [] // No permissions granted
});
7. File System Access:
// For file picker
const [fileChooser] = await Promise.all([
page.waitForEvent('filechooser'),
page.click('#upload')
]);
await fileChooser.setFiles('./document.pdf');
💡 Tips:
- Set permissions trong context creation
- Different permissions cho different test scenarios
- Mock permission prompts để avoid manual interaction
- Test both granted và denied states
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