The synchronization of UiAutomator

强颜欢笑 提交于 2019-12-01 07:27:53

问题


I am writing uiautomator test case for running Android app program in emulator. Here comes the problem, assume I run the Ui test case in a different emulator machine. How could I ensure the record machine and the playback machine react in the same speed. For instance, the playback machine may react slower than the record machine, So when the test case triggers a click action in a button, the playback machine may not have loaded that button in the layout. Is there any mechanism in uiautomator which could always synchronize the playing of the test cases and the machine reaction? I am afraid if the playback machine is too slow, then some unfound exception may be thrown out.


回答1:


Generally you use functions like UiDevice.wait(..) to pause until your test can proceed.

For example, if you have a button which opens a screen containing some elements you want to interact with, you'll need to wait for the new content to appear before trying to interact with those elements.

This would look something like:

detailsButton.click();

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

// Do something on the details pane



回答2:


Allen Hair answer works just fine if you know what to expect after the click. If you don't (like in my case) you can:

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.

I do this on top of

device.waitForWindowUpdate(packageName, timeOut)

because in several occasions the wait would return and the root node of the screen could not yet be accessed, resulting in future UiObjectNotFoundException.



来源:https://stackoverflow.com/questions/31377936/the-synchronization-of-uiautomator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!