Search code examples
androidseleniumautomationappium

Selenium isSelected() method for CheckBox always return false for Android


I have a list of checkboxes in my Android app, that i need to test. So when i use isSelected() method, it always return false, no matter if this Checkbox is checked or not. Was trying to use findelementById() and byXpath(). Same result.

Here is the parts of the code what i used:

WebElement checkBox = driver.findElementById(appType + id);
    if (!checkBox.isSelected()){
        Reporter.log(backupDataType + " checkbox isn't checked. clicking on it...", true);
        ...}

Using Xpath:

checkBox = driver.findElementByXPath("//android.widget.CheckBox[@resource-id='" + appType + checkSms + "']");
    if (!driver.findElementByXPath("//android.widget.CheckBox[@resource-id='" + appType + checkSms + "']").isSelected()){
    Reporter.log(backupDataType + " checkbox isn't checked. clicking on it...", true);
    ...}

Path to the element is correct, because it's always clicking on it. No matter, if it checked, or not.


Solution

  • Considering you are using an android checkbox widget, you shall try using the getAttribute property of a WebElement as follows-

    WebElement element = <find element by your locator strategy>; // in your case  driver.findElementByXPath("//android.widget.CheckBox[@resource-id='" + appType + checkSms + "']");
    String checkboxAttribute = element.getAttribute("checked");
    if(!checkboxAttribute.equalsIgnoreCase("true")) {
       Reporter.log(backupDataType + " checkbox isn't checked. clicking on it...", true);
       ...
    }
    

    P.S - try and practice saving a WebElement instead of finding it again.