0
0
Selenium Javatesting~5 mins

Checkbox handling in Selenium Java

Choose your learning style9 modes available
Introduction

Checkbox handling helps you test if checkboxes on a webpage work correctly. It ensures users can select or deselect options as expected.

Testing a signup form where users agree to terms by checking a box.
Verifying multiple filters on a shopping site can be selected or cleared.
Checking if a newsletter subscription checkbox toggles correctly.
Ensuring a 'Remember me' checkbox on login works as intended.
Syntax
Selenium Java
WebElement checkbox = driver.findElement(By.id("checkboxId"));

// To check if checkbox is selected
boolean isSelected = checkbox.isSelected();

// To select checkbox if not already selected
if (!isSelected) {
    checkbox.click();
}

// To deselect checkbox if selected
if (isSelected) {
    checkbox.click();
}

Use isSelected() to check checkbox state before clicking.

Clicking toggles the checkbox state.

Examples
Selects the checkbox named 'subscribe' only if it is not already selected.
Selenium Java
WebElement checkbox = driver.findElement(By.name("subscribe"));
if (!checkbox.isSelected()) {
    checkbox.click();
}
Deselects the checkbox for 'terms' if it is currently selected.
Selenium Java
WebElement checkbox = driver.findElement(By.cssSelector("input[type='checkbox'][value='terms']"));
if (checkbox.isSelected()) {
    checkbox.click();
}
Sample Program

This test opens a webpage, finds a checkbox by its ID, selects it if not already selected, prints the action, and asserts the checkbox is selected.

Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class CheckboxTest {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();
        try {
            driver.get("https://example.com/form");

            WebElement checkbox = driver.findElement(By.id("agreeTerms"));

            // Check if checkbox is selected
            if (!checkbox.isSelected()) {
                checkbox.click();
                System.out.println("Checkbox was not selected, now selected.");
            } else {
                System.out.println("Checkbox was already selected.");
            }

            // Verify checkbox is selected
            assert checkbox.isSelected() : "Checkbox should be selected after clicking.";

        } finally {
            driver.quit();
        }
    }
}
OutputSuccess
Important Notes

Always check the checkbox state before clicking to avoid unwanted toggling.

Use unique and stable locators like ID or name for checkboxes.

Remember to close the browser after tests to free resources.

Summary

Checkbox handling tests if checkboxes can be selected or deselected correctly.

Use isSelected() to check state before clicking.

Clicking toggles the checkbox state.