Android 什么';在我的uiautomator测试中,有比睡觉更好的解决方法吗?

Android 什么';在我的uiautomator测试中,有比睡觉更好的解决方法吗?,android,android-uiautomator,Android,Android Uiautomator,下面是我从Nexus5设备的设置帐户中得到的uiautomator测试的一个片段。测试用例输入电子邮件地址、密码,然后单击Next。然后休眠2秒钟,并检查后续帐户设置屏幕上是否有另一个“下一步”按钮 请注意,我已经使用了“clickAndWaitForNewWindow()”,所以我不知道为什么如果我没有2秒钟的睡眠,测试会失败。只要睡2秒钟,它就会过去。我怀疑这是因为当您单击“下一步”时,设备将显示一个弹出窗口“连接到网络…”,并且clickAndWaitForNewWindow()将完成,即

下面是我从Nexus5设备的设置帐户中得到的uiautomator测试的一个片段。测试用例输入电子邮件地址、密码,然后单击Next。然后休眠2秒钟,并检查后续帐户设置屏幕上是否有另一个“下一步”按钮

请注意,我已经使用了“clickAndWaitForNewWindow()”,所以我不知道为什么如果我没有2秒钟的睡眠,测试会失败。只要睡2秒钟,它就会过去。我怀疑这是因为当您单击“下一步”时,设备将显示一个弹出窗口“连接到网络…”,并且clickAndWaitForNewWindow()将完成,即使我正在查找的新窗口尚未出现,并且在assertTrue时将失败

有谁有更好的解决办法吗?我试图避免使用sleep()的做法,是否希望使用更好的逻辑,如waitForCondition(timeout)


我找到了解决办法。你可以 nextButton.waitForExists(5000)

这将等待“下一步”按钮出现。它将在5秒后超时

// Add a new account = enter email address, password, and click on "Next" button
new UiObject(new UiSelector().index(1)).setText("myaccount@hotmail.com");
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());