Here is a snippet of the uiautomator test I have from the Settings account of a Nexus 5 device. The test case enters the email address, password, then click Next. Then it sleeps for 2 seconds and check that there is another "Next" button on the subsequent account setup screen.
Note that I used "clickAndWaitForNewWindow()" already so I am not sure why if I don't have the 2 seconds sleep, the test would fail. With the 2 second sleep, it would pass. I suspect this is because when you click "Next", the device would show a popup "Connecting to network ..." and the clickAndWaitForNewWindow() completes even though the new window that I was looking for is not there yet and would fail at the assertTrue.
Does anyone have a better workaround? I am trying to avoid the practice of using sleep() and would like a better logic like waitForCondition(timeout)?
// Add a new account = enter email address, password, and click on "Next" button
new UiObject(new UiSelector().index(1)).setText("[email protected]");
new UiObject(new UiSelector().index(2)).setText("mypassword");
UiObject nextButton = new UiObject(new UiSelector().text("Next"));
assertTrue("Next not found or enabled", nextButton.exists() && nextButton.isEnabled());
nextButton.clickAndWaitForNewWindow();
sleep(2000);
// There should be another screen with the account settings and user needs to press "Next" button again
assertTrue("Next not found or enabled in Account Settings", nextButton.exists() && nextButton.isEnabled());
I found a solution. You could do nextButton.waitForExists(5000);
This would wait for the Next button to appear. It will time out after 5 seconds.