Sunday, November 23, 2014

Selenium API - Working with CheckBox and Radio Buttons

Important Classes and methods :

RadioButton.get(0).isSelected();

CheckBox.get(i).getAttribute("value");



// Store all the elements of same category in the list of WebLements
List  oRadioButton = driver.findElements(By.name("toolsqa"));
// Create a boolean variable which will hold the value (True/False)
boolean bValue = false;
// This statement will return True, in case of first Radio button is selected
bValue = oRadioButton.get(0).isSelected();
// This will check that if the bValue is True means if the first radio button is selected
if(bValue = true){
// This will select Second radio button, if the first radio button is selected by default
oRadioButton.get(1).click();
}else{
// If the first radio button is not selected by default, the first will be selected
oRadioButton.get(0).click();
}




WITH VALUE:


List oCheckBox = driver.findElements(By.name("tool"));
// This will tell you the number of checkboxes are present
int iSize = oCheckBox.size();
// Start the loop from first checkbox to last checkboxe
for(int i=0; i < iSize ; i++ ){
// Store the checkbox name to the string variable, using 'Value' attribute
String sValue = oCheckBox.get(i).getAttribute("value");
// Select the checkbox it the value of the checkbox is same what you are looking for
if (sValue.equalsIgnoreCase("toolsqa")){
oCheckBox.get(i).click();
// This will take the execution out of for loop
break;
}
}

No comments:

Post a Comment