0
0
Selenium Javatesting~10 mins

Unexpected alert handling in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test checks how the Selenium WebDriver handles an unexpected alert popup during navigation. It verifies that the alert is detected and accepted to allow the test to continue smoothly.

Test Code - JUnit 5 with Selenium WebDriver
Selenium Java
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class UnexpectedAlertHandlingTest {
    private WebDriver driver;
    private WebDriverWait wait;

    @BeforeEach
    public void setUp() {
        driver = new ChromeDriver();
        wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    }

    @Test
    public void testHandleUnexpectedAlert() {
        driver.get("https://example.com/page_with_unexpected_alert");

        try {
            // Wait for alert to be present
            Alert alert = wait.until(ExpectedConditions.alertIsPresent());
            // Accept the alert
            alert.accept();
        } catch (TimeoutException e) {
            // No alert appeared, continue
        }

        // After alert is handled, verify page title
        String title = driver.getTitle();
        assertEquals("Expected Page Title", title);
    }

    @AfterEach
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensChrome browser window is open and ready-PASS
2Navigates to https://example.com/page_with_unexpected_alertBrowser loads the page which triggers an unexpected alert popup-PASS
3Waits up to 10 seconds for alert to be present using WebDriverWait and ExpectedConditions.alertIsPresent()Alert popup is detected on the pageAlert presence is verifiedPASS
4Accepts the alert popup by calling alert.accept()Alert popup is closed, page is visible and interactive-PASS
5Retrieves the page title using driver.getTitle()Page title is fetched from the browserPage title equals 'Expected Page Title'PASS
6Test ends and browser closesBrowser window is closed-PASS
Failure Scenario
Failing Condition: Alert does not appear and test tries to accept a non-existent alert or page title does not match expected
Execution Trace Quiz - 3 Questions
Test your understanding
What Selenium method is used to wait for the unexpected alert to appear?
Await.until(ExpectedConditions.alertIsPresent())
Bdriver.findElement(By.alert())
Cdriver.switchTo().alert() without wait
Ddriver.getAlert()
Key Result
Always use explicit waits like WebDriverWait with ExpectedConditions to handle unexpected alerts gracefully, preventing test failures due to timing issues.