0
0
Selenium Javatesting~20 mins

Alert accept and dismiss in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Alert Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
APrints null
BNo output is printed because accept() closes the alert silently
CThrows NoAlertPresentException
DThe text inside the alert box is printed
Attempts:
2 left
💡 Hint
Remember that getText() reads the alert message before accepting it.
assertion
intermediate
1: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();
AassertEquals("Cancelled", labelText);
BassertTrue(labelText.contains("Cancelled"));
CassertFalse(labelText.equals("Cancelled"));
DassertNull(labelText);
Attempts:
2 left
💡 Hint
Check exact match of the label text after dismissing the alert.
🔧 Debug
advanced
2: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();
ANo alert is present on the page when switchTo().alert() is called
BThe alert is already accepted before calling accept()
CThe alert text is empty
DThe driver instance is null
Attempts:
2 left
💡 Hint
Check if the page actually shows an alert before switching.
framework
advanced
2: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?
AUse implicit wait set to 10 seconds
BUse Thread.sleep(5000) before switching to alert
CUse WebDriverWait with ExpectedConditions.alertIsPresent()
DUse FluentWait ignoring NoAlertPresentException without condition
Attempts:
2 left
💡 Hint
Look for a wait condition specifically for alerts.
🧠 Conceptual
expert
2: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?
AThe prompt throws an exception because input is required
BThe prompt is closed and the input value is null or empty
CThe prompt accepts with default text automatically
DThe prompt remains open until input is sent
Attempts:
2 left
💡 Hint
Dismiss means canceling the prompt without input.