👉Handling Alerts and Popups:
- Mastering Alerts & Popups in Selenium WebDriver: A Complete Guide.
- How to Handle JavaScript Alerts & Popups in Selenium Like a Pro.
- Selenium WebDriver Alerts Handling: Best Practices & Examples.
- Step-by-Step Guide to Managing Popups in Selenium WebDriver.
- Handling Browser Alerts in Selenium: Everything You Need to Know.
👉Introduction:
In web automation testing using Selenium WebDriver, handling alerts and popups is a crucial skill. Web applications frequently use JavaScript-based alerts, confirmation boxes, and prompt dialogs to interact with users. As testers, understanding how to efficiently manage these alerts ensures smooth execution of automated test scripts.
This guide will walk you through various types of alerts in Selenium WebDriver and how to handle them using different methods. We will also cover how to manage popup windows and switch between multiple windows.
👉What is an Alert in Selenium?
An alert in Selenium is a small JavaScript-based message box that appears on the screen. It provides users with important information, warnings, or requests for input. Alerts can be broadly categorized into three types:
- Simple Alert
- Confirmation Alert
- Prompt Alert
Each type of alert requires different handling techniques, which we will explore in this tutorial.
👉Types of Alerts in Selenium and How to Handle Them:
👉1) Simple Alert:
A simple alert displays an informational message and usually contains only an “OK” button.
How to Handle Simple Alert in Selenium:
// Switching to Alert
Alert alert = driver.switchTo().alert();
// Capturing alert message
String alertMessage = alert.getText();
System.out.println("Alert message: " + alertMessage);
// Accepting (Clicking OK) on alert
alert.accept();
👉2) Confirmation Alert:
A confirmation alert asks the user for confirmation before proceeding. It has two options: OK and Cancel.
How to Handle Confirmation Alert:
// Switching to alert
Alert confirmationAlert = driver.switchTo().alert();
// Getting alert text
String alertText = confirmationAlert.getText();
System.out.println("Confirmation Alert Text: " + alertText);
// Accepting alert (Clicking OK)
confirmationAlert.accept();
// Dismissing alert (Clicking Cancel)
// confirmationAlert.dismiss();
👉3) Prompt Alert:
A prompt alert asks the user for input. It has an input box along with “OK” and “Cancel” buttons.
How to Handle Prompt Alert:
// Switching to alert
Alert promptAlert = driver.switchTo().alert();
// Entering text in alert
promptAlert.sendKeys("Test Input");
// Accepting alert
promptAlert.accept();
👉Handling Popups in Selenium WebDriver:
Apart from alerts, popups can appear in different forms like authentication popups, file upload dialogs, and advertisement popups. Selenium WebDriver provides different approaches to handling them.
👉Handling Browser Authentication Popups:
Authentication popups require entering a username and password. These can be handled by embedding credentials in the URL.
// Format: http://username:password@URL
String URL = "http://admin:admin@the-internet.herokuapp.com/basic_auth";
driver.get(URL);
👉Handling Multiple Windows (Popup Windows):
Sometimes clicking a button opens a new browser window. Selenium provides methods to switch between multiple windows.
How to Switch Between Windows:
// Get parent window handle
String parentWindow = driver.getWindowHandle();
// Click on a button that opens a new window
driver.findElement(By.id("open-new-window")).click();
// Get all window handles
Set<String> windowHandles = driver.getWindowHandles();
for (String windowHandle : windowHandles) {
if (!windowHandle.equals(parentWindow)) {
driver.switchTo().window(windowHandle);
System.out.println("Switched to new window");
}
}
// Close the new window and switch back to the main window
driver.close();
driver.switchTo().window(parentWindow);
👉Best Practices for Handling Alerts and Popups in Selenium:
- Use Explicit Waits: Sometimes alerts take time to appear. Use
WebDriverWait
to handle them efficiently.WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); wait.until(ExpectedConditions.alertIsPresent());
- Handle Unexpected Alerts: Unexpected alerts may break the script. Use
try-catch
blocks to manage them.try { Alert unexpected Alert = driver. switch To().alert(); unexpected Alert .dismiss(); } catch (No Alert Present Exception e) { System .out. print ln("No alert found"); }
- Verify Alert Messages: Always validate alert messages to ensure they contain expected text before proceeding.
- Use Appropriate Switching Methods: Switch back to the main window after handling alerts or popups.
👉Conclusion:
Handling alerts and popups is a key aspect of Selenium automation testing. By understanding different types of alerts—simple, confirmation, and prompt—and using the appropriate Selenium WebDriver methods, you can efficiently manage popups and alerts in your test scripts. Implementing best practices like explicit waits, exception handling, and window switching will enhance the reliability of your automation scripts.