2014-07-03 12 views
2

我是新來的這個論壇和appium/android自動化,我需要幫助驗證我的應用程序中是否存在一個對象,然後再執行下一個操作。Appium - 如何驗證對象是否存在

我嘗試使用下面的代碼,但甚至沒有達到我的第二個打印語句。

@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控制檯輸出...它只是保持循環通過此和腳本失敗。

信息:自舉] [資訊]類型的行動得到了命令

信息:自舉] [調試]得到命令操作:找到

信息:自舉] [調試]尋找約翰使用NAME DOE與關聯標識符:

信息:[BOOTSTRAP] [信息]返回結果:{ 「值」: 「未找到元件」, 「狀態」:7}

信息:推命令appium工作隊列:[「find」,{「strategy」:「name」,「s選項「:」John Doe「,」context「:」「,」multiple「:false}]

info:[BOOTSTRAP] [info]得到客戶端的數據:{」cmd「:」action「,」action 「:」 發現」, 「PARAMS」:{ 「策略」: 「名字」, 「選擇」: 「李四」, 「語境」: 「」, 「多」:假}}

是isDisplayed這裏有沒有正確的方法或者是否有更好的選擇來做到這一點?

乾杯.... TIA

回答

1

如果您正在使用Appium 1.0+

  • By.name定位策略一直deprecated.Please使用一些其他的東西像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')]")));