Challenge - 5 Problems
Alert Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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"); }
Attempts:
2 left
💡 Hint
Think about what happens when an alert blocks interaction in Selenium.
✗ Incorrect
When an unexpected alert appears, Selenium throws UnhandledAlertException. The catch block prints "Alert detected and handled".
❓ assertion
intermediate1: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();
Attempts:
2 left
💡 Hint
Exact match requires equality assertion.
✗ Incorrect
assertEquals checks exact equality, which is needed here. Other options check contains, false equality, or null which are incorrect.
🔧 Debug
advanced2: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"); }
Attempts:
2 left
💡 Hint
Consider when the alert appears relative to the click.
✗ Incorrect
If alert appears before click, the click() throws UnhandledAlertException because alert was blocking interaction. The code never reaches switchTo().alert(), and UnhandledAlertException is not caught.
❓ framework
advanced2:00remaining
Best practice to handle unexpected alerts in Selenium tests?
Which approach is best to handle unexpected alerts globally in Selenium Java tests?
Attempts:
2 left
💡 Hint
Think about a scalable way to handle alerts without repeating code.
✗ Incorrect
Event listeners allow centralized handling of alerts, improving maintainability and reducing code duplication.
🧠 Conceptual
expert1: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?
Attempts:
2 left
💡 Hint
Selenium blocks interaction when alert is present.
✗ Incorrect
Selenium throws UnhandledAlertException when an unexpected alert blocks interaction, causing test failure unless handled.