How to verify the number of check box is checked/ Selected in webPage ?
Below Example:
import java.util.List;
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 MultipleCheckBoxVerify {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("someURL");
List<WebElement> checkBoxesele = driver.findElements(By.xpath("//input[@type='Checkbox']"));
for(int i=0; i<checkBoxes.size(); i=i+2){
checkBoxesele.get(i).click();
}
int checkedCount=0, uncheckedCount=0;
for(int i=0; i<checkBoxesele.size(); i++){
System.out.println(i+" checkbox is selected "+checkBoxesele.get(i).isSelected());
if(checkBoxesele.get(i).isSelected()){
checkedCount++;
}else{
uncheckedCount++;
}
}
System.out.println("count of selected checkbox: "+checkedCount);
System.out.println("count of unselected checkbox: "+uncheckedCount);
}
}
o/p:
0 checkbox is selected true
1 checkbox is selected false
2 checkbox is selected true
3 checkbox is selected false
4 checkbox is selected true
5 checkbox is selected false
count of selected checkbox: 3
count of unselected checkbox: 3
- Navigate to the site first and collect all the checkbox elements by using findElements() method.
- Store the value in a list of web elements.
- Find the size of list of elements the validate with conditions.
Below Example:
import java.util.List;
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 MultipleCheckBoxVerify {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("someURL");
List<WebElement> checkBoxesele = driver.findElements(By.xpath("//input[@type='Checkbox']"));
for(int i=0; i<checkBoxes.size(); i=i+2){
checkBoxesele.get(i).click();
}
int checkedCount=0, uncheckedCount=0;
for(int i=0; i<checkBoxesele.size(); i++){
System.out.println(i+" checkbox is selected "+checkBoxesele.get(i).isSelected());
if(checkBoxesele.get(i).isSelected()){
checkedCount++;
}else{
uncheckedCount++;
}
}
System.out.println("count of selected checkbox: "+checkedCount);
System.out.println("count of unselected checkbox: "+uncheckedCount);
}
}
o/p:
0 checkbox is selected true
1 checkbox is selected false
2 checkbox is selected true
3 checkbox is selected false
4 checkbox is selected true
5 checkbox is selected false
count of selected checkbox: 3
count of unselected checkbox: 3