Modern web applications frequently use JavaScript dialogs and browser popups to interact with users. These include alert messages, confirmation dialogs, prompt windows, and new browser tabs.
Handling these correctly is essential for building reliable automation frameworks.
In this article, we will learn how to handle alerts, confirmations, and browser popups in Playwright using Java.
Types of Dialogs in Web Applications
The most common dialog types are:
- Alert Dialog
- Confirmation Dialog
- Prompt Dialog
- Browser Popup Window
Let's explore each one with examples.
Handling JavaScript Alert Dialogs
Alert dialogs display information to users and usually contain only an OK button.
Example:
page.onDialog(dialog -> {
System.out.println(dialog.message());
dialog.accept();
});
page.locator("#showAlert").click();Output:
Operation completed successfully.The accept() method clicks the OK button automatically.
Handling Confirmation Dialogs
Confirmation dialogs usually provide two options:
- OK
- Cancel
Example:
page.onDialog(dialog -> {
System.out.println(dialog.message());
dialog.accept();
});
page.locator("#deleteButton").click();This accepts the confirmation and proceeds with the action.
Dismissing Confirmation Dialogs
Sometimes your test needs to verify the Cancel flow.
Example:
page.onDialog(dialog -> {
dialog.dismiss();
});
page.locator("#deleteButton").click();The dismiss() method clicks the Cancel button.
Handling Prompt Dialogs
Prompt dialogs allow users to enter input values.
Example:
page.onDialog(dialog -> {
dialog.accept("Gnanendra");
});
page.locator("#promptButton").click();This enters the value "Gnanendra" and clicks OK.
Reading Dialog Messages
Validating dialog messages is a common testing requirement.
Example:
page.onDialog(dialog -> {
String actualMessage = dialog.message();
System.out.println(actualMessage);
dialog.accept();
});You can compare the message with the expected value using assertions.
Handling Browser Popups
Many applications open new windows for:
- Payment gateways
- Reports
- Help pages
- External authentication providers
Playwright provides built-in support for popup windows.
Example:
Page popupPage = page.waitForPopup(() -> {
page.locator("#openReport").click();
});
System.out.println(popupPage.title());This waits for the new browser window to appear and captures its reference.
Working with Multiple Browser Tabs
Once the popup is captured, you can interact with it like any other page.
Example:
popupPage.locator("#downloadButton").click();
popupPage.close();This allows automation across multiple browser windows.
Handling Authentication Popups
Some applications display browser authentication dialogs.
Playwright supports HTTP authentication using browser context options.
Example:
BrowserContext context =
browser.newContext(
new Browser.NewContextOptions()
.setHttpCredentials(
"admin",
"password123"
)
);This automatically handles authentication challenges.
Common Mistake: Clicking Before Registering the Listener
Incorrect approach:
page.locator("#alertButton").click();
page.onDialog(dialog -> {
dialog.accept();
});The dialog appears before Playwright starts listening.
Correct approach:
page.onDialog(dialog -> {
dialog.accept();
});
page.locator("#alertButton").click();Always register the dialog listener first.
Best Practices
Register listeners before actions
Always attach the dialog handler before triggering the dialog.
Validate dialog messages
Verify business messages whenever possible.
Examples:
- "Order placed successfully"
- "Are you sure you want to delete?"
- "Payment completed"
Use popup references carefully
Avoid using the parent page reference after switching to popup windows.
Close unnecessary windows
Closing popup windows prevents resource leaks during long test executions.
Complete Example
page.onDialog(dialog -> {
System.out.println(
"Dialog Message: " +
dialog.message()
);
dialog.accept();
});
page.locator("#submitOrder").click();
Page popup =
page.waitForPopup(() -> {
page.locator("#viewReceipt").click();
});
System.out.println(
popup.title()
);
popup.close();This example handles both dialogs and popup windows in the same test.
Interview Questions
What is the difference between accept() and dismiss()?
accept()clicks OK.dismiss()clicks Cancel.
How do you enter text into a prompt dialog?
Use:
dialog.accept("text value");How do you handle multiple browser windows in Playwright?
Use:
page.waitForPopup()to capture the new window reference.
Conclusion
Handling alerts and popups is an essential skill for every automation engineer.
Playwright simplifies these operations through built-in APIs and event listeners.
In this article, you learned:
- How to handle alerts
- How to handle confirmations
- How to handle prompt dialogs
- How to work with browser popups
- Best practices for dialog handling
Mastering these techniques will make your Playwright automation framework more robust and production-ready.
In the next article, we will learn how to upload and download files in Playwright using Java.
No comments:
Post a Comment