How to verify that a checkbox is 'checked'?

2,671 views
Skip to first unread message

Andrew Dinh

unread,
Dec 5, 2013, 2:20:00 PM12/5/13
to appium-...@googlegroups.com
Hi all,
 
I am writing a Java script to verify if a checkbox is 'checked' on an Android view.
 
So on the view, I got a list of WebElements with tagName CheckBox.  Then I try to verify if the first checkbox (the only one) is 'checked'.
 
List<WebElement> checkboxes = driver.findElements(By.tagName("CheckBox"));
     if (checkboxes.get(0).isSelected()) { 
             my code
     }
 
But I got the following error:
org.openqa.selenium.WebDriverException: Returned value cannot be converted to Boolean: false
 
I googled the above error message and found the isSelected method:
 
  public boolean isSelected() {
    Object value = execute(DriverCommand.IS_ELEMENT_SELECTED, ImmutableMap.of("id", id))
        .getValue();
    try {
      return (Boolean) value;
    } catch (ClassCastException ex) {
      throw new WebDriverException("Returned value cannot be converted to Boolean: " + value, ex);
    }

  }

So, basically there is a ClassCastException when WebDriver was trying to convert the returned value to Boolean?  Do you  know what I should do to correct this?  Thanks. 

Andrew Dinh

unread,
Dec 5, 2013, 2:37:26 PM12/5/13
to appium-...@googlegroups.com
Googled and found some suggestions.  Tried this version and it seemed to work for checkboxes.  Not sure about radio buttons though.
 
if (checkboxes.get(0).getAttribute("checked") != null) {
       my codes
}

Andrew Dinh

unread,
Dec 5, 2013, 6:50:25 PM12/5/13
to appium-...@googlegroups.com
I think there is something wrong with the getAttribute method.  Should it have returned the strings "true" and "false" instead of the Boolean 'true' and 'false'?
 
The reason I asked is because when the checkbox is checked, the 'if' block ran
 
if (checkboxes.get(0).getAttribute("checked") != null) {
       my codes
}
else {
       test case fails
}
 
However, when the checkbox is empty, the 'if' block is ALSO ran.
 
After some hair pulling, I found the code below behaves correctly, i.e., empty checkbox will fail the test case. 
 
if (checkboxes.get(0).getAttribute("checked").equals("true")) {
       my codes
}
else {
       test case fails
}
 
This leads me to suspect that the returned values for checked and unchecked checkboxes are the strings "true" and "false" respectively.  Is my thinking correct and, if yes, is this as designed or is it a bug?  Thanks.

bootstrap online

unread,
Dec 5, 2013, 6:53:27 PM12/5/13
to Andrew Dinh, appium-...@googlegroups.com
You should convert them to boolean. Boolean.parseBoolean(...)

I'm not sure if they should be strings.
> --
> http://appium.io
> ---
> You received this message because you are subscribed to the Google Groups
> "Appium-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to appium-discus...@googlegroups.com.
> Visit this group at http://groups.google.com/group/appium-discuss.
> For more options, visit https://groups.google.com/groups/opt_out.

Andrew Dinh

unread,
Dec 5, 2013, 7:15:50 PM12/5/13
to appium-...@googlegroups.com, Andrew Dinh
Hi bootstrap,
 
I think I am okay.  As long as my script is doing the correct thing, I am happy :).  My purpose is only to check the checkboxes or radio buttons for the correct states.  It really does not matter whether the method returns a string or Boolean value. 
 
Thanks for your suggestion!
Reply all
Reply to author
Forward
0 new messages