Challenge - 5 Problems
Alert Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output after accepting the alert?
Consider the following Selenium Java code snippet that handles a JavaScript alert. What will be the printed output after the alert is accepted?
Selenium Java
WebDriver driver = new ChromeDriver();
driver.get("https://example.com/alert");
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
alert.accept();
System.out.println(alertText);Attempts:
2 left
💡 Hint
Remember that getText() reads the alert message before accepting it.
✗ Incorrect
The getText() method retrieves the alert message before the alert is accepted. So the printed output is the alert's text.
❓ assertion
intermediate1:30remaining
Which assertion correctly verifies alert dismissal?
You want to verify that after dismissing an alert, a label on the page shows "Cancelled". Which assertion is correct?
Selenium Java
Alert alert = driver.switchTo().alert();
alert.dismiss();
String labelText = driver.findElement(By.id("resultLabel")).getText();Attempts:
2 left
💡 Hint
Check exact match of the label text after dismissing the alert.
✗ Incorrect
The label should exactly show "Cancelled" after dismissing the alert, so assertEquals is the best choice.
🔧 Debug
advanced2:00remaining
Why does this alert accept code throw NoAlertPresentException?
Analyze the code below. Why does it throw NoAlertPresentException at runtime?
Selenium Java
driver.get("https://example.com/noalert");
Alert alert = driver.switchTo().alert();
alert.accept();Attempts:
2 left
💡 Hint
Check if the page actually shows an alert before switching.
✗ Incorrect
NoAlertPresentException occurs because the code tries to switch to an alert that does not exist on the page.
❓ framework
advanced2:00remaining
Which Selenium wait strategy best handles alerts appearing asynchronously?
You want to wait for an alert to appear before accepting it. Which Selenium wait approach is best?
Attempts:
2 left
💡 Hint
Look for a wait condition specifically for alerts.
✗ Incorrect
WebDriverWait with ExpectedConditions.alertIsPresent() waits efficiently for alert presence before switching.
🧠 Conceptual
expert2:30remaining
What happens if you call dismiss() on a prompt alert without sending input?
Consider a JavaScript prompt alert that asks for user input. What is the expected behavior if you call dismiss() without sending any text?
Attempts:
2 left
💡 Hint
Dismiss means canceling the prompt without input.
✗ Incorrect
Calling dismiss() on a prompt closes it and returns null or empty input, simulating user cancel action.