Reputation: 419
How to ckeck the checkbox is enabled or not? In selenium + Testng, in application im clicking on checkbox will be enabled, i need to verify whether the checkbox is enabled or not.thanks in advance.
Upvotes: 3
Views: 24191
Reputation: 4424
There's a method "isEnabled()", that checks whether a WebElement is enabled or not. You can use the below code to check for that;
boolean enabled = driver.findElement(By.xpath("//xpath of the checkbox")).isEnabled();
( I have used xpath above but you can use id or cssselector for locating the element, too.)
The above code will return 'true', if the concerned WebElement, i.e., in your case the checkbox, is enabled, else it will return 'false'.
And, in case you want to check whether the checkbox is checked/selected or not, you can use "isSelected()" method, which you can use like this;
boolean checked = driver.findElement(By.xpath("//xpath of the checkbox")).isSelected();
The above code will return 'true', if the concerned WebElement, i.e., in your case the checkbox, is checked, else it will return 'false'.
Taking your code snippet in the comment, I have conjured up a method below:-
This will return a string "Pass" if the checkbox is checked, and "Fail" if it isn't checked or in case any error comes up while executing this code.
public static String isCheckBoxChecked(String objlocator, String elemName) {
APP_LOGS.debug("Checking if the checkbox related to '"+elemName+"' is checked or not.");
System.out.println("Checking if the checkbox related to '"+elemName+"' is checked or not.");
try {
findWebElement(objlocator);
//Assuming the objLocator contains xpath
if (driver.findElement(By.xpath(objlocator)).isSelected()) {
System.out.println("Checkbox related to: '"+elemName+"' is checked.");
APP_LOGS.debug("Checkbox related to: '"+elemName+"' is checked.");
}else{
System.out.println("Checkbox related to: '"+elemName+"' is not checked!!");
APP_LOGS.debug("Checkbox related to: '"+elemName+"' is not checked!!");
return "Fail" + ": Checkbox related to: '"+elemName+"' is not checked!!";
}
}
catch (Throwable t) {
System.out.println("Error while Checking if the checkbox related to '"+elemName+"' is checked or not. -" + t.getMessage());
APP_LOGS.error("Error while Checking if the checkbox related to '"+elemName+"' is checked or not. -" + t.getMessage());
return "Fail"+": Error while Checking if the checkbox related to '"+elemName+"' is checked or not. -" + t.getMessage();
}
return "Pass"+": Checkbox related to: '"+elemName+"' is checked.";
}
Upvotes: 6
Reputation: 3851
//Checking
public void CheckingChkbox(WebElement chkbx1){
boolean checkstatus;
checkstatus=chkbx1.isSelected();
if (checkstatus==true){
System.out.println("Checkbox is already checked");
}
else
{
chkbx1.click();
System.out.println("Checked the checkbox");
}
}
//Unchecking
public void UnCheckingChkbox(WebElement chkbx1){
boolean checkstatus;
checkstatus=chkbx1.isSelected();
if (checkstatus==true) {
chkbx1.click();
System.out.println("Checkbox is unchecked");
}
else
{
System.out.println("Checkbox is already unchecked");
}
}
http://seleniumcodes.blogspot.in/2011/10/checking-and-unchecking-checkbox.html
Upvotes: 0
Reputation: 45
use this if you :
bool flag = false;
if (checkBox1.Checked == true)
flag = true;
Upvotes: 0
Reputation: 754
Use the following code. This will return a boolean value based on if check box is checked or not. Based on that you can either click on the checkbox. Happy coding.
boolean selected = element.isSelected();
if(!selected)
{
element.click();
}
Upvotes: 0