selenium tutorial testing tools

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());
		}
	}

}
Tags: 
Selenium Tutorials: 

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.

Wanted Contributers

Hello Viewer! We're a relatively a small group of writers who have combined efforts to write the articles. Well, we need more help from talented people to contribute and grow the size of articles. It's great work if you're looking for a little money on the side – make this as your Part time job.

We are mainly looking into Protractor, Cucumber, JBehave, RSpec, Behat Mink, Selenium Python or any other tool which uses selenium.

If you can contribute on any the above, Please contact us.