Java Selenium UnhandledAlertException - Handling Unexpected Alerts

Selenium is a powerful tool for automating web browsers, enabling testers and developers to execute various web-based tests. However, during web automation, you may encounter the "UnhandledAlertException," which occurs when an unhandled alert is present on the web page.

Understanding UnhandledAlertException

The UnhandledAlertException is thrown when a JavaScript alert, confirmation, or prompt dialog is present on the web page, and it has not been handled by the test script. When an unhandled alert is encountered, Selenium cannot perform any further actions on the page until the alert is accepted, dismissed, or handled appropriately.

Common Causes

Several common causes can lead to UnhandledAlertException:

  • JavaScript alerts: Websites often use JavaScript alerts for various purposes, such as displaying messages or confirming actions.
  • Unexpected alerts: Alerts may appear unexpectedly during the test execution, disrupting the test flow.
  • Asynchronous events: Asynchronous events or AJAX calls may trigger alerts during test execution.
  • Alert race conditions: Race conditions may occur when handling multiple alerts simultaneously.
  • Timing issues: The test script may not handle the alert in time before moving to the next step.

Handling UnhandledAlertException

To handle UnhandledAlertException effectively, consider the following best practices:

  1. Use explicit waits: Implement explicit waits to ensure alerts are handled before proceeding with other test steps.
  2. Try-catch blocks: Use try-catch blocks to catch UnhandledAlertException and handle the alert appropriately.
  3. Switch to the alert: Use the WebDriver switchTo() method to switch to the alert and perform actions on it.
  4. Accept or dismiss the alert: Accept or dismiss the alert based on the desired test scenario.
  5. Use headless mode: Consider using headless mode to prevent alert display during test execution.

Example Code

Here's an example of how to handle UnhandledAlertException in Java Selenium:


import org.openqa.selenium.Alert;
import org.openqa.selenium.UnhandledAlertException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class UnhandledAlertExample {
public static void main(String[] args) {
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-notifications"); // Disable browser notifications

scss
Copy code
    WebDriver driver = new ChromeDriver(options);
    driver.get("https://techoral.com");

    try {
        // Perform actions that trigger an alert
        // ...
        // Switch to the alert and handle it
        Alert alert = driver.switchTo().alert();
        alert.accept(); // To accept the alert
        // Or
        alert.dismiss(); // To dismiss the alert
    } catch (UnhandledAlertException e) {
        // Handle the exception or report the failure
    }

    driver.quit();
}
}

Conclusion

UnhandledAlertException is a common exception faced during web automation using Java Selenium. Understanding its causes and implementing best practices to handle it can lead to more robust and reliable test scripts. By using explicit waits, try-catch blocks, and switching to the alert, you can effectively handle unexpected alerts and ensure smoother and more successful web automation.





Read Next :






Subscribe to Our Newsletter

Get the latest updates and exclusive content delivered to your inbox!