Working with Checkbox using Resuable Methods

Working with checkbox is very simple using webdriver. It is same as click operation. But it is always recommended to check if that is already in selected mode or deselected. Because, if it is already selected and when you click on the same element it will get deselected. We will look into different ways to perform select and de-select operations on checkboxes. We will also use reusable methods to perform the operation in multiple tests.

The below is the simple command to do that:

WebElement checkBoxElement=driver.findElement(By.id("persist_box"));
checkBoxElement.click();

I will try to explain you both select and de-select with the sample code:

package com.pack.methods;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;

public class CheckBoxExample {
	
	private WebDriver driver;
	private String basePageURL;
	
	@Test
	public void testCaseToCheck() {
		driver = new FirefoxDriver();
		driver.get(basePageURL);
		WebElement checkBoxElement=driver.findElement(By.id("persist_box"));
		//Wait for the checkbox element to be visible
		new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOf(checkBoxElement));
		Select_The_Checkbox(checkBoxElement);
	}

	@Test
	public void testCaseToUnCheck() {
		driver.navigate().to(basePageURL);
		WebElement checkBoxElement=driver.findElement(By.id("persist_box"));
		new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOf(checkBoxElement));
		DeSelect_The_Checkbox(checkBoxElement);
	}
	
	@Test
	public void testCaseToCheckDesired(){
		driver.navigate().to("someother page");
		WebElement element = driver.findElement(By.cssSelector(".display"));
		Select_The_CheckBox_from_List(element, "soccer");
	}

In the above program, we have used reusable methods to select, deselect and select a particular value from multiple checkboxes. It is always better to have resubale to methods, so that we can resuse the same methods in multiple tests instead of writing the same code in multiple tests to perform the same operation.

Select_The_Checkbox(checkBoxElement);
DeSelect_The_Checkbox(checkBoxElement);
Select_The_CheckBox_from_List(element, "soccer");

Below are the resuable methods that are used in the above example code. You can write these methods in a separate class called generics or utils class where you can have all the resuable methods like below

Below method is used to Select a Checkbox, if it is not selected already

	public void Select_The_Checkbox(WebElement element) {
		try {
            if (element.isSelected()) {
               System.out.println("Checkbox: " + element + "is already selected");
            } else {
            	// Select the checkbox
                element.click();
            }
        } catch (Exception e) {
        	System.out.println("Unable to select the checkbox: " + element);
        }
		
	}

Below method is used to De-select a Checkbox, if it is selected already

	public void DeSelect_The_Checkbox(WebElement element) {
		try {
            if (element.isSelected()) {
            	//De-select the checkbox
                element.click();
            } else {
            	System.out.println("Checkbox: "+element+"is already deselected");
            }
        } catch (Exception e) {
        	System.out.println("Unable to deselect checkbox: "+element);
        }
    }		

Below method is used to select the checkbox with the specified value from multiple checkboxes.

	public void Select_The_CheckBox_from_List(WebElement element, String valueToSelect) {
		List<WebElement> allOptions = element.findElements(By.tagName("input"));
		for (WebElement option : allOptions) {
			   System.out.println("Option value "+option.getText());
			        if (valueToSelect.equals(option.getText())) {
			            option.click();
			            break;
			        }
			    }
	}
	
}
Selenium Tutorials: 

Add new comment