2015-07-13 41 views
1

我正在編寫用於在模擬器中運行Android應用程序的uiautomator測試用例。問題在於,假設我在不同的仿真器中運行Ui測試用例。我如何確保錄製機器和播放機器以相同的速度作出反應。例如,回放機器可能會比記錄機器反應慢。因此,當測試用例在按鈕中觸發點擊操作時,回放機器可能尚未在佈局中加載該按鈕。 uiautomator中是否有任何機制可以始終使測試用例的播放和機器響應同步?我擔心如果播放機速度太慢,那麼可能會拋出一些無法理解的異常。UiAutomator的同步

回答

3

通常,您可以使用UiDevice.wait(..)之類的功能暫停,直到您的測試可以繼續。例如,如果您有一個按鈕打開包含要與之交互的某些元素的屏幕,則在嘗試與這些元素進行交互之前,您需要等待新內容出現。

這看起來是這樣的:如果你知道點擊後會發生什麼

detailsButton.click(); 

device.wait(Until.hasObject(By.desc("Details Pane")), TIMEOUT); 

// Do something on the details pane 
+0

非常感謝。這幫助我嘗試調用虛擬方法'void android.support.test.uiautomator.UiObject2。'在一個空對象引用錯誤!因爲Uiautomator在下一個屏幕加載前退出 – gorbysbm

+0

即使等待成功,它也會給我NPE。出於某種原因,「AccessibilityNodeInfo」中只有空的子元素,因此無法找到我的按鈕。看起來像一些錯誤。 – WindRider

0

艾倫發答案的作品就好了。如果不這樣做(如在我的情況),您可以:

UiAutomation uiAuto = InstrumentationRegistry.getIntrumentation().getUiAutomation(); //gets the UiAutomation for this execution 
AccessibilityNodeInfo rootNode = uiAuto.getRootInActiveWindow(); //gets the root node of the currently visible screen 
//makes sure there is a rootNode and that is has children, i.e., there is a drawn screen 
while(rootNode == null || rootNode.getChildCount() == 0){ 
    rootNode = uiAuto.getRootInActiveWindow(); 
} 
//getting here you are sure that the new screen is drawn. 

我因爲多次做這個的

device.waitForWindowUpdate(packageName, timeOut) 

頂部等待將返回並在屏幕的根節點可能尚未被訪問,導致將來出現UiObjectNotFoundException。