Bird
0
0

Which code snippet correctly uses a variable for input and verifies the result?

hard📝 Application Q9 of 15
Selenium Java - Handling Windows, Frames, and Alerts
In a test, you need to enter a dynamic value into a prompt alert and then verify the page updates accordingly. Which code snippet correctly uses a variable for input and verifies the result? ```java String userInput = "Tester"; Alert prompt = driver.switchTo().alert(); prompt.sendKeys(userInput); prompt.accept(); String message = driver.findElement(By.id("msg")).getText(); assertTrue(message.contains(userInput)); ```
Adriver.findElement should be called before prompt.accept()
BsendKeys cannot accept variables, only literals
CassertTrue is not valid in Selenium tests
DThis code correctly inputs variable and asserts page update
Step-by-Step Solution
Solution:
  1. Step 1: Use variable input in sendKeys()

    sendKeys() accepts string variables, so userInput is valid input.
  2. Step 2: Verify assertion usage

    assertTrue() is a valid assertion method to check if message contains userInput.
  3. Final Answer:

    This code correctly inputs variable and asserts page update -> Option D
  4. Quick Check:

    Variable input + assertTrue = correct test code [OK]
Quick Trick: sendKeys accepts variables; assertTrue checks conditions [OK]
Common Mistakes:
  • Thinking sendKeys only accepts string literals
  • Misusing assertion methods
  • Calling findElement before alert acceptance

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes