2014-10-05 122 views
2

在我們的uiautomator測試用例中,我們使用UiObject.clickAndWaitForNewWindow觸發任何點擊操作。然而,有時候設備響應/加載頁面的時間過長。因此,在頁面完全加載之前,任何後續分析/測試都會失敗,因爲預期的小部件尚未準備好。我們以某種未確定的方式失敗了一些測試用例。大部分時間只需重新運行他們通過的測試用例。UiObject clickAndWaitForNewWindow加載時間過長

有什麼方法可以確保UiObject.clickAndWaitForNewWindow在繼續下一次驗證之前完全加載期望的頁面嗎?我們可以在UiObject.clickAndWaitForNewWindow之後插入一個睡眠語句,它作爲雙刃劍使用,但並不能真正解決這個問題。

UiObject.clickAndWaitForNewWindow(); 
// how to wait until the page is loaded completely 
new UiSelector(...); // search for widgets that are supposed to be on the newly loaded page 

回答

0

這裏的一種方法是在這裏添加一個while循環。

說,當你在一個窗口中的一些元素上點擊,然後會出現一個新的屏幕,你想在新的窗口中的一些元素(比如菜單)上執行操作和元素定義爲 -

//say you tapped on some element(say abc) on the first window 
abc.click(); 

//now you want to wait till the element (say menu) appears on the next screen which is decl as - 
UiObject menu = new UiObject(new UiSelector().resourceId("com.example.test:id/elmnt")); 

//add a while loop to check if that element exists and add a print statement just to keep printing till that element appears on the screen- 
while (!menu.exists()) { 

    System.out.println("Menu will appear in some time"); 

} 

System.out.println("Menu appeared"); 
//carry other operations here 
+0

感謝Smriti! 這假定我們知道資源ID「com.example.test:id/elmnt」。如果我們不呢?或者更糟糕的是,我們不知道任何特殊的小部件可以幫助我們識別新的頁面。 – user908645 2014-10-08 07:12:39

+0

也許我應該這樣問,反正有,就像ctor在返回之前構造一個完整的對象一樣,在ui automator中告訴所有的小部件已經準備好在新的頁面上? – user908645 2014-10-08 07:15:30

0

我正在使用

UiDevice.waitForWindowUpdate(package, timeout); 

它到目前爲止工作正常。有了這個,你不需要任何元素的id。我認爲還有沒有超時的選項。