Verify checkbox selection and deselection on the preferences page
Preconditions (2)
✅ Expected Result: The checkbox can be selected and deselected correctly, reflecting the user's choice.
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import java.time.Duration; import static org.junit.jupiter.api.Assertions.*; public class CheckboxHandlingTest { private WebDriver driver; private WebDriverWait wait; @BeforeEach public void setUp() { driver = new ChromeDriver(); wait = new WebDriverWait(driver, Duration.ofSeconds(10)); driver.get("https://example.com/preferences"); } @Test public void testCheckboxSelectionAndDeselection() { By newsletterCheckboxLocator = By.id("receiveNewsletter"); // Wait until checkbox is visible and enabled WebElement checkbox = wait.until(ExpectedConditions.elementToBeClickable(newsletterCheckboxLocator)); // Verify checkbox is initially not selected assertFalse(checkbox.isSelected(), "Checkbox should not be selected initially"); // Click to select checkbox checkbox.click(); // Verify checkbox is selected assertTrue(checkbox.isSelected(), "Checkbox should be selected after clicking"); // Click again to deselect checkbox checkbox.click(); // Verify checkbox is not selected assertFalse(checkbox.isSelected(), "Checkbox should not be selected after clicking again"); } @AfterEach public void tearDown() { if (driver != null) { driver.quit(); } } }
This test uses Selenium WebDriver with JUnit 5 to automate checkbox handling.
Setup: We open the browser and navigate to the preferences page.
Wait: We wait explicitly until the checkbox is clickable to avoid timing issues.
Assertions: We check the checkbox is initially not selected, then click it and verify it is selected, then click again and verify it is deselected.
Teardown: We close the browser after the test to clean up.
This approach ensures the test is stable and easy to maintain.
Now add data-driven testing with 3 different checkboxes: 'Receive newsletter', 'Enable notifications', 'Accept terms'.
isSelected() method do when working with checkboxes in Selenium Java?isSelected()isSelected() only checks state.driver.findElement(By.id("subscribe")) to locate the checkbox by its ID..click() on the found element to toggle checkbox state.WebElement checkbox = driver.findElement(By.id("agree"));
if (!checkbox.isSelected()) {
checkbox.click();
}
System.out.println(checkbox.isSelected());
What will be printed if the checkbox was initially unchecked?isSelected() returns false.checkbox.click() runs, selecting it.isSelected() returns true, so true is printed.WebElement cb = driver.findElement(By.id("option1"));
if (cb.isSelected()) {
cb.click();
}
But the checkbox remains unselected after running. What is the error?if (!cb.isSelected()) { cb.click(); } to select checkbox.isSelected() to verify if checkbox is already selected.Pass the test and this pattern moves unknown