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 co
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
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.