0
0
Selenium Javatesting~15 mins

Checkbox handling in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Verify checkbox selection and deselection on the preferences page
Preconditions (2)
Step 1: Locate the checkbox labeled 'Receive newsletter'
Step 2: Check if the checkbox is not selected
Step 3: Click the checkbox to select it
Step 4: Verify the checkbox is selected
Step 5: Click the checkbox again to deselect it
Step 6: Verify the checkbox is not selected
✅ Expected Result: The checkbox can be selected and deselected correctly, reflecting the user's choice.
Automation Requirements - Selenium WebDriver with JUnit 5
Assertions Needed:
Verify checkbox is initially not selected
Verify checkbox is selected after clicking
Verify checkbox is not selected after clicking again
Best Practices:
Use explicit waits to ensure checkbox is interactable
Use By.id or By.cssSelector for locating checkbox
Use Page Object Model to separate page elements and test logic
Use assertions from JUnit 5 for validation
Automated Solution
Selenium Java
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.

Common Mistakes - 3 Pitfalls
Using Thread.sleep() instead of explicit waits
Locating checkbox with brittle XPath like absolute paths
Not verifying checkbox state before and after clicking
Bonus Challenge

Now add data-driven testing with 3 different checkboxes: 'Receive newsletter', 'Enable notifications', 'Accept terms'.

Show Hint