Working with Alerts and Pop-ups in Playwright
Working with Alerts and Pop-ups in Playwright
When automating web applications using Playwright, you’ll often encounter JavaScript alerts, confirm boxes, and prompt pop-ups. These dialogs can interrupt the flow of automation if not handled correctly. Playwright provides simple and efficient ways to deal with these browser dialogs.
In this blog, we’ll cover:
What types of pop-ups Playwright can handle
How to handle alerts, confirms, and prompts
Example scripts for each scenario
๐งพ Types of JavaScript Dialogs
Alert – A simple message with an OK button
Confirm – A message with OK and Cancel buttons
Prompt – A message with an input field, OK, and Cancel buttons
๐งฐ Playwright’s Way of Handling Pop-ups
Playwright uses the page.on("dialog") event to listen for any dialog box that appears on the page.
javascript
Copy
Edit
page.on('dialog', async dialog => {
console.log(dialog.message());
await dialog.accept(); // or dialog.dismiss() for Cancel
});
Let’s look at examples for each type of dialog.
✅ Handling JavaScript Alerts
javascript
Copy
Edit
await page.goto('https://example.com');
page.on('dialog', async dialog => {
console.log("Alert message: ", dialog.message());
await dialog.accept();
});
await page.click('#alertButton'); // Triggers alert
⚠️ Handling Confirm Dialogs
javascript
Copy
Edit
page.on('dialog', async dialog => {
console.log("Confirm message: ", dialog.message());
await dialog.accept(); // Accept = Click OK
// await dialog.dismiss(); // Dismiss = Click Cancel
});
await page.click('#confirmButton');
✍️ Handling Prompt Dialogs
javascript
Copy
Edit
page.on('dialog', async dialog => {
console.log("Prompt message: ", dialog.message());
await dialog.accept('Playwright User'); // Entering text and clicking OK
});
await page.click('#promptButton');
๐งช Things to Keep in Mind
Always set the page.on('dialog') before the action that triggers the alert.
If you don’t handle a dialog, Playwright will throw an error and fail the test.
Use dialog.dismiss() if you want to simulate clicking "Cancel".
๐งฉ Use Case: Automating an Alert Workflow
javascript
Copy
Edit
test('Handle all dialog types', async ({ page }) => {
await page.goto('https://your-test-site.com');
page.on('dialog', async dialog => {
const type = dialog.type();
console.log(`Dialog type: ${type}`);
if (type === 'alert') {
await dialog.accept();
} else if (type === 'confirm') {
await dialog.dismiss();
} else if (type === 'prompt') {
await dialog.accept('Sample input');
}
});
await page.click('#showAlert');
await page.click('#showConfirm');
await page.click('#showPrompt');
});
✅ Conclusion
Handling JavaScript alerts and pop-ups is essential for building robust Playwright automation scripts. Whether you're dealing with simple alerts or interactive prompts, Playwright makes it easy to intercept and respond to them programmatically.
Learn Playwright Training Course in Hyderabad
Read More
Handling Frames and Iframes Using Playwright
Page Object Model (POM) with Playwright
Visit Our IHUB Talent Training Institute in Hyderabad
Comments
Post a Comment