0
0
Selenium Javatesting~20 mins

Prompt alert text entry in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Prompt Alert Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output after entering text in a prompt alert?
Consider this Selenium Java code snippet that handles a prompt alert. What will be the printed output after execution?
Selenium Java
WebDriver driver = new ChromeDriver();
driver.get("https://example.com/prompt");
Alert prompt = driver.switchTo().alert();
prompt.sendKeys("Hello");
prompt.accept();
String result = driver.findElement(By.id("result")).getText();
System.out.println(result);
AUnhandledAlertException
BHello
CNoSuchElementException
Dnull
Attempts:
2 left
💡 Hint
The prompt alert accepts the input text and the page updates the element with id 'result' to show the entered text.
assertion
intermediate
1:30remaining
Which assertion correctly verifies prompt alert text entry?
You want to verify that entering 'Test123' in a prompt alert updates the page text correctly. Which assertion is correct?
Selenium Java
Alert prompt = driver.switchTo().alert();
prompt.sendKeys("Test123");
prompt.accept();
String actualText = driver.findElement(By.id("output")).getText();
AassertFalse(actualText.isEmpty());
BassertTrue(actualText.contains("Test"));
CassertNotNull(actualText);
DassertEquals("Test123", actualText);
Attempts:
2 left
💡 Hint
You want to check exact match of the entered text.
🔧 Debug
advanced
2:30remaining
Why does this prompt alert text entry code fail with UnhandledAlertException?
Review this code snippet. It throws UnhandledAlertException at runtime. What is the cause?
Selenium Java
driver.get("https://example.com/prompt");
WebElement button = driver.findElement(By.id("showPrompt"));
button.click();
String alertText = driver.switchTo().alert().getText();
System.out.println(alertText);
driver.findElement(By.id("inputField")).sendKeys("Hello");
driver.switchTo().alert().accept();
AThe alert is accepted before sendKeys causing the exception.
BThe alert text is not retrieved correctly causing the exception.
CTrying to interact with page element while prompt alert is open causes UnhandledAlertException.
DThe button click does not open the prompt alert.
Attempts:
2 left
💡 Hint
You cannot interact with page elements while an alert is open.
framework
advanced
2:30remaining
Which Selenium Java code snippet correctly enters text into a prompt alert and verifies the result?
Select the code snippet that correctly handles a prompt alert by entering 'QA Test', accepting it, and asserting the page shows the entered text.
A
Alert alert = driver.switchTo().alert();
alert.sendKeys("QA Test");
alert.accept();
String text = driver.findElement(By.id("message")).getText();
assertEquals("QA Test", text);
B
Alert alert = driver.switchTo().alert();
alert.accept();
alert.sendKeys("QA Test");
String text = driver.findElement(By.id("message")).getText();
assertEquals("QA Test", text);
C
driver.findElement(By.id("promptInput")).sendKeys("QA Test");
Alert alert = driver.switchTo().alert();
alert.accept();
String text = driver.findElement(By.id("message")).getText();
assertEquals("QA Test", text);
D
Alert alert = driver.switchTo().alert();
alert.dismiss();
alert.sendKeys("QA Test");
String text = driver.findElement(By.id("message")).getText();
assertEquals("QA Test", text);
Attempts:
2 left
💡 Hint
You must send keys before accepting the alert.
🧠 Conceptual
expert
3:00remaining
What is the correct sequence to handle a prompt alert text entry in Selenium Java?
Arrange the steps in the correct order to enter text into a prompt alert and confirm it using Selenium Java.
A1,2,3,4
B1,3,2,4
C2,1,3,4
D3,1,2,4
Attempts:
2 left
💡 Hint
You must switch to alert before sending keys and accept after sending keys.