0
0
Selenium Javatesting~20 mins

Unexpected alert handling in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Alert Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output when an unexpected alert appears?
Consider the following Selenium Java code snippet. What will be the output when an unexpected alert pops up during the click action?
Selenium Java
try {
    driver.findElement(By.id("submitBtn")).click();
    System.out.println("Clicked successfully");
} catch (UnhandledAlertException e) {
    System.out.println("Alert detected and handled");
}
AAlert detected and handled
BClicked successfully
CNo output, test hangs
DThrows NoSuchElementException
Attempts:
2 left
💡 Hint
Think about what happens when an alert blocks interaction in Selenium.
assertion
intermediate
1:30remaining
Which assertion correctly verifies alert text?
You want to verify that an alert's text is exactly "Session expired". Which assertion is correct?
Selenium Java
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
AassertEquals(alertText, "Session expired");
BassertTrue(alertText.contains("Session expired"));
CassertFalse(alertText.equals("Session expired"));
DassertNull(alertText);
Attempts:
2 left
💡 Hint
Exact match requires equality assertion.
🔧 Debug
advanced
2:30remaining
Why does this code fail to handle an unexpected alert?
This code tries to accept an unexpected alert but fails with UnhandledAlertException. Identify the cause.
Selenium Java
try {
    driver.findElement(By.id("saveBtn")).click();
    driver.switchTo().alert().accept();
} catch (NoAlertPresentException e) {
    System.out.println("No alert to accept");
}
Adriver.findElement throws exception, not handled
BNoAlertPresentException is not caught properly
Caccept() method is deprecated and causes failure
DAlert appears before click, click() throws UnhandledAlertException before switchTo().alert()
Attempts:
2 left
💡 Hint
Consider when the alert appears relative to the click.
framework
advanced
2:00remaining
Best practice to handle unexpected alerts in Selenium tests?
Which approach is best to handle unexpected alerts globally in Selenium Java tests?
AIgnore alerts and let tests fail to fix issues manually
BWrap every click action in try-catch for UnhandledAlertException
CUse WebDriver event listeners to detect and accept alerts automatically
DUse Thread.sleep() before every action to wait for alerts
Attempts:
2 left
💡 Hint
Think about a scalable way to handle alerts without repeating code.
🧠 Conceptual
expert
1:30remaining
What happens if you ignore an unexpected alert in Selenium?
If a test encounters an unexpected alert and does not handle it, what is the most likely outcome?
ATest ignores alert and continues normally
BTest throws UnhandledAlertException and stops execution
CAlert automatically closes after timeout
DTest logs warning but passes
Attempts:
2 left
💡 Hint
Selenium blocks interaction when alert is present.