Check Box

What is a Check Box?

A checkbox menu is a graphical user interface element that presents a list of checkbox options allowing the user to select one or more choices.

Some key characteristics of checkbox menus:

  • Contains multiple checkboxes as options instead of radio buttons or list items.

  • Each checkbox represents one choice or option that can be individually selected.

  • The user can select any number of checkboxes from the list.

  • Checkboxes have two states - checked or unchecked. Clicking toggles the state.

  • Provides a way to select multiple options from a set of choices.

  • Often used to enable/disable certain settings or preferences.

  • Can allow selection of zero, one, or more options.

  • Selected options remain checked until explicitly unchecked.

  • More flexible than radio buttons or dropdowns for multi-selection.

  • The visual indicator (check mark) shows which options are currently selected.

  • Commonly placed next to each option but can also be shown separately.

Sample Checkbox Project

package org.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;

public class Checkbox {
    public static void main(String[] args) throws InterruptedException {

        //1     Check the first  Checkbox and verify if it is successfully checked and
        //2     Uncheck it again to verify if it is successfully Unchecked
        //3     How to get the Count of number of check boxes present in the page

        System.setProperty("webdriver.chrome.driver", "/Users/optionstrillionaire/Downloads/chromedriver");
        WebDriver driver = new ChromeDriver();
        driver.get("<https://rahulshettyacademy.com/AutomationPractice/>");

        //1
        Assert.assertFalse(driver.findElement(By.cssSelector("input#checkBoxOption1")).isSelected());
        driver.findElement(By.cssSelector("input#checkBoxOption1")).click();
        Assert.assertTrue(driver.findElement(By.cssSelector("input#checkBoxOption1")).isSelected());
        //2
        driver.findElement(By.cssSelector("input#checkBoxOption1")).click();
        Assert.assertFalse(driver.findElement(By.cssSelector("input#checkBoxOption1")).isSelected());
        //3
        System.out.println(driver.findElements(By.cssSelector("input[type='checkbox']")).size());

        driver.quit();

    }
}

Last updated