Handle confirm and prompt dialogs
intermediate Alerts Challenge 11 of 13
The drafts page uses native browser dialogs. Clicking Delete (#delete-draft) opens a confirm("Delete this draft?"), and clicking Rename (#rename-draft) opens a prompt("New name for this draft:"). The current test sleeps and never touches the dialogs, so it fails with UnhandledAlertException.
Requirements
- Remove every
Thread.sleep. - After each click, wait with
ExpectedConditions.alertIsPresent()and get the dialog withdriver.switchTo().alert(). - Assert the confirm’s text, then dismiss it so the draft is kept.
- Assert the prompt’s text, type
Q3 regression planinto it and accept it. - Finally assert that
#draft-titleshows the new name.
Expected output
Each click is followed by `wait.until(ExpectedConditions.alertIsPresent())` and `driver.switchTo().alert()`. The confirm's text is asserted and the confirm is dismissed; the prompt's text is asserted, it receives `sendKeys("Q3 regression plan")` and is accepted. There is no `Thread.sleep`.
Checks (6)
- ○No Thread.sleep
- ○Waits for the dialog with ExpectedConditions.alertIsPresent()
- ○Switches to the dialog with driver.switchTo().alert()
- ○Dismisses (cancels) the confirm
- ○Types into the prompt and then accepts it
- ○Reads the dialog text
Solution.javaValidated · not executed
Ctrl/⌘ + Enter to validate · your code is saved in this browser
Results
Validate your code against the checks.
Related interview questions
- Checked vs unchecked exceptions: how should a test framework handle exceptions?
- How do you handle StaleElementReferenceException in Selenium?
- Using Java streams, find the duplicate values in a list and count their occurrences.
- How do you make a Selenium framework thread-safe for parallel execution? Explain ThreadLocal.