首页 文章

Appium - 如何验证对象是否存在

提问于
浏览
2

我是这个论坛和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信息的命令:[BOOTSTRAP] [debug]得到命令动作:查找信息:[BOOTSTRAP] [debug]使用带有contextId的NAME查找John Doe:info:[BOOTSTRAP] [ info]返回结果:{“value”:“找不到元素”,“status”:7} info:将命令推送到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}}

is isDisplayed在这里是正确的方法还是有更好的替代方法来做到这一点?

干杯...... TIA

3 回答

  • 1

    如果您使用的是Appium 1.0

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

    在较新版本的appium中,您有“辅助功能ID” . 请使用这些 . 快乐的自动化

  • 0

    也许以下内容对您有所帮助 . 我有我的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')]")));
    

相关问题