【问题标题】:Appium - How to Validate if an object existsAppium - 如何验证对象是否存在
【发布时间】:2014-07-03 00:15:50
【问题描述】:

我是这个论坛和 appium/android 自动化的新手,在采取下一步行动之前,我需要帮助来验证我的应用中是否存在对象。

我尝试使用下面的代码,但甚至没有达到我的第二个打印语句。

@Test
public void addContact() {
    System.out.println( "Checking if Contact exists.... 111111 ");
   
    WebElement e = driver.findElement(By.name("John Doe"));
           
    System.out.println( "Checking if Contact exists.... 222222");
   
    boolean contactExists = e.isDisplayed();
   
    System.out.println( contactExists );
   
   
    if (contactExists == true) {          
        System.out.println( "Contact exists.... ");           
    } else {           
        System.out.println( "Contact DOES NOT exists.... ");
    }
 }

在此处运行此代码是 appium 控制台输出...它只是不断循环通过此代码并且脚本失败。

info: [BOOTSTRAP] [info] 得到了 ACTION 类型的命令

info: [BOOTSTRAP] [debug] Got command action: find

info: [BOOTSTRAP] [debug] 使用 NAME 和 contextId 查找 John Doe:

info: [BOOTSTRAP] [info] 返回结果:{"value":"No element found","status":7}

信息:将命令推送到 appium 工作队列:["find",{"strategy":"name","selector":"John Doe","context":"","multiple":false}]

info: [BOOTSTRAP] [info] 从客户端获取数据:{"cmd":"action","action":"find","params":{"strategy":"name","selector": "John Doe","context":"","multiple":false}}

isDisplayed 是正确的方法还是有更好的替代方法?

干杯.... TIA

【问题讨论】:

    标签: android appium


    【解决方案1】:

    如果您使用的是 Appium 1.0+

    • By.name 定位策略已被弃用。请使用其他一些东西,如 By.xpath 等。

    【讨论】:

      【解决方案2】:

      在较新版本的 appium 中,您有“可访问性 id”。请改用这些。 快乐的自动化

      【讨论】:

        【解决方案3】:

        也许以下内容会对您有所帮助。我的 TestBase 类中有方法:

            protected static boolean isElementPresent(By by) {
            driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
            List<WebElement> list = driver.findElements(by);
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            if (list.size() == 0) {
                return false;
            } else {
                return list.get(0).isDisplayed();
            }
        
        }
        
        public boolean elementIsNotPresent(By by) {
            try {
                driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
                return driver.findElements(by).isEmpty();
            } finally {
                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            }
        }
        

        另外,我使用以下代码来等待屏幕上的某个特定元素:

            WebDriverWait wait = new WebDriverWait(driver, 30);
            wait.until(ExpectedConditions.elementToBeClickable(By
                    .xpath("//android.widget.Button[contains(@text, 'Log In')]")));
        

        或:

        WebDriverWait wait = new WebDriverWait(driver, 30);
        wait.until(ExpectedConditions.presenceOfElementLocated(By
                    .xpath("//android.widget.TextView[contains(@resource-id, 'action_bar_title')]")));
        

        【讨论】:

          猜你喜欢
          • 2022-11-21
          • 2021-04-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-10-09
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多