Checkboxes are widely used in the web application, mostly in those situations where the user needs to choose one or more available options. For example, when you fill up any company registration form, there are one or more checkbox options for language selection. In this case, you have to select many available checkbox options for language selection as English, Hindi, Bengali, etc.

Selenium WebDriver provides support for checkboxes through the WebElement class. A typical example of checkboxes in the web application is shown in the below screenshot.
Checkbox in Selenium
CheckBox operations are easy to perform in the testing web application. We will use two important methods click() and isSelected() provided by Selenium WebDriver in test script to interact with checkbox elements.
Click command: It is used for clicking on checkboxes and on radio button.
So, let's automate a web application where we will select on one or more checkboxes.

Scenario to be automated:
1. Open webpage "https://seleniumtesting08.blogspot.com/2019/07/check-box-and-radio-buttons.html".
2. Locate checkbox elements "Red", "Blue", and "Orange", and click on them for selecting.
Let's see the following source code related to this scenario. 
Program source code 1:
    package practiceCheckbox; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class CheckBox { public static void main(String[] args) { // Declare WebDriver reference. WebDriver driver; // Create an object of FirefoxDriver class. driver=new FirefoxDriver(); // or WebDriver diver=new FirefoxDriver(); // Maximize browser. driver.manage().window().maximize(); String URL="https://seleniumtesting08.blogspot.com/2019/07/check-box-and-radio-buttons.html"; driver.get(URL); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Locate checkboxes "Red", "Blue", "Orange". WebElement red=driver.findElement(By.xpath("//input[@value='red']")); red.click(); System.out.println("Red Checkbox is selected"); WebElement blue=driver.findElement(By.xpath("//input[@value='blue']")); blue.click(); System.out.println("Blue Checkbox is selected"); WebElement orange=driver.findElement(By.xpath("//input[@value='orange']")); orange.click(); System.out.println("Orange Checkbox is selected"); driver.close(); } }
    Output: Red Checkbox is selected Blue Checkbox is selected Orange Checkbox is selected

How to check whether Checkbox selected or not using Selenium WebDriver?


During test execution, checkboxes are selected or not, it can be verified using isSelected() command in assertTure. If the checkbox is selected, it will send a message "Checkbox is selected" otherwise it fails the step and stop further execution of test. Let's see the program source code.


Program source code 2:
    package checkboxPrograms; import org.testng.annotations.Test; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.Assert; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeClass; public class CheckboxChecking { WebDriver driver; @BeforeClass public void setUp() { driver=new FirefoxDriver(); driver.manage().window().maximize(); String URL="https://seleniumtesting08.blogspot.com/2019/07/check-box-and-radio-buttons.html"; driver.get(URL); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); } @Test public void assertTure(){ // Locates checkbox "Red". WebElement red=driver.findElement(By.xpath("//input[@value='red']")); red.click(); Assert.assertTrue(red.isSelected()); System.out.println("Red checkbox is selected"); } @AfterMethod public void afterMethod() { driver.close(); } }
    Output: Red checkbox is selected PASSED: assertTure =============================================== Default test Tests run: 1, Failures: 0, Skips: 0 ===============================================
In the preceding example program, If the assert statement is successfully executed, the next statement will be executed otherwise the next statement will not execute and message "Red checkbox is selected" will not print on console.

The above program can also be executed without using TestNG. In this case, we will use if else statement instead of assertTure.
Program source code 3:
    package checkboxProgram; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.BeforeClass; public class VerifyCheckbox { public static void main(String[] args) { WebDriver driver; driver=new FirefoxDriver(); driver.manage().window().maximize(); String URL="https://seleniumtesting08.blogspot.com/2019/07/check-box-and-radio-buttons.html"; driver.get(URL); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); WebElement red=driver.findElement(By.xpath("//input[@value='red']")); red.click(); if(red.isSelected()){ System.out.println("Red checkbox is selected"); } else { System.out.println("Something went wrong"); } driver.close(); } }
    Output: Red checkbox is selected
Final words
Hope that this tutorial has covered almost all important points related to checkbox in Selenium WebDriver. I hope that you will have understood this topic easily with doing practical and enjoyed it.