【问题标题】:Selenium isSelected() method for CheckBox always return false for AndroidCheckBox 的 Selenium isSelected() 方法对于 Android 始终返回 false
【发布时间】:2016-10-15 22:11:56
【问题描述】:

我的 Android 应用中有一个复选框列表,我需要对其进行测试。因此,当我使用 isSelected() 方法时,无论是否选中此复选框,它总是返回 false。 试图使用 findelementById() 和 byXpath()。结果相同。

这是我使用的部分代码:

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

使用 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);
    ...}

元素的路径是正确的,因为它总是在点击它。没关系,是否检查。

【问题讨论】:

    标签: android selenium automation appium


    【解决方案1】:

    考虑到您使用的是 android 复选框小部件,您应尝试使用 WebElementgetAttribute 属性,如下所示 -

    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 - 尝试并练习保存 WebElement 而不是再次找到它。

    【讨论】:

    • 谢谢你的工作!我通常会保存网页元素。在这种情况下,我只是尝试不同的组合来让它发挥作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-25
    • 2014-10-13
    • 1970-01-01
    • 2018-01-26
    相关资源
    最近更新 更多