Working with Multiple Checkboxes with Safecheck

We have seen working with checkboxes with different examples to select and de-select the checkboxes using selenium webdriver Select class Now in this tutorial we will working with multiple checkboxes.

There are cases were we need to select multiple checkboxes or de-select multiple checkboxes. Below is the sample code to work with multiple checkboxes.

We will pass two parameters, One is the wait time to wait for element to be clickable and then perform the check safely ONLY when the Checkbox element is not selected

package com.pack.checkbox;	

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.StaleElementReferenceException;
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 {
	WebDriver driver;

	@Test
	public void testMultipleCheckBox() throws Exception {
		driver = new FirefoxDriver();
		driver.manage().window().maximize();
		driver.navigate().to("App URL");

		WebElement element1 = driver.findElement(By.cssSelector("input[value=itunes]"));
		WebElement element2 = driver.findElement(By.cssSelector("input[value=lastfm]"));
		WebElement element3 = driver.findElement(By.cssSelector("input[value=spotify]"));
		WebElement[] elements = { element1, element2, element3 };
		safeSelectCheckBoxes(10, elements);
	}

	public void safeSelectCheckBoxes(int waitTime, WebElement... elements) throws Exception {
		WebElement checkElement = null;
		try {
			if (elements.length > 0) {
				for (WebElement currentElement : elements) {
					checkElement = currentElement;
					WebDriverWait wait = new WebDriverWait(driver, waitTime);
					wait.until(ExpectedConditions.elementToBeClickable(currentElement));

					WebElement checkBox = currentElement;
					if (checkBox.isSelected())
						System.out.println("CheckBox " + currentElement
								+ " is already selected");
					else
						checkBox.click();
				}
			} else {
				System.out
						.println("Expected atleast one element as argument to safeSelectCheckboxes function");
			}
		} catch (StaleElementReferenceException e) {
			System.out.println("Element - " + checkElement
					+ " is not attached to the page document "
					+ e.getStackTrace());
		} catch (NoSuchElementException e) {
			System.out.println("Element " + checkElement + " was not found in DOM"
					+ e.getStackTrace());
		} catch (Exception e) {
			System.out
					.println("Unable to select checkbox " + e.getStackTrace());
		}
	}

}
Selenium Tutorials: 

Comments

How to select more than one item in a bounded view combo box whose ID is dynamically changing..?
I have used XPATH to access such dynamically changing elements. But am unable to select more than one item from a list of elements .

Add new comment