Bird
0
0

You want to verify that a checkbox with id "newsletter" is checked before submitting a form. Which code snippet correctly asserts this using JUnit in Selenium Java?

hard📝 Application Q9 of 15
Selenium Java - Handling Form Elements
You want to verify that a checkbox with id "newsletter" is checked before submitting a form. Which code snippet correctly asserts this using JUnit in Selenium Java?
AWebElement checkbox = driver.findElement(By.id("newsletter")); assertEquals(checkbox.getText(), "checked");
BWebElement checkbox = driver.findElement(By.id("newsletter")); assertTrue(checkbox.isSelected());
CWebElement checkbox = driver.findElement(By.id("newsletter")); assertFalse(checkbox.isSelected());
DWebElement checkbox = driver.findElement(By.id("newsletter")); assertNull(checkbox);
Step-by-Step Solution
Solution:
  1. Step 1: Locate checkbox by id

    Use driver.findElement(By.id("newsletter")) to get the checkbox element.
  2. Step 2: Assert checkbox is selected

    Use JUnit's assertTrue() with checkbox.isSelected() to verify it is checked.
  3. Final Answer:

    assertTrue(checkbox.isSelected()); -> Option B
  4. Quick Check:

    Assert checkbox checked = assertTrue(isSelected()) [OK]
Quick Trick: Use assertTrue with isSelected() to verify checkbox checked [OK]
Common Mistakes:
  • Using assertFalse instead of assertTrue
  • Checking getText() for checkbox state
  • Asserting null on checkbox element

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes