这是我从 Nexus 5 设备的“设置”帐户中获得的 uiautomator 测试 fragment 。测试用例输入邮箱地址、密码,然后点击下一步。然后它会休眠 2 秒,并检查在随后的帐户设置屏幕上是否还有另一个“下一步”按钮。
请注意,我已经使用了“clickAndWaitForNewWindow()”,所以我不确定为什么如果我没有 2 秒的 sleep 时间,测试就会失败。随着2秒的 sleep ,它会过去。我怀疑这是因为当你点击“下一步”时,设备会显示一个弹出窗口“连接到网络......”并且 clickAndWaitForNewWindow() 完成,即使我正在寻找的新窗口还不存在并且会失败断言为真。
有没有人有更好的解决方法?我试图避免使用 sleep() 的做法,并希望有更好的逻辑,如 waitForCondition(timeout)?
// 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());
最佳答案
我找到了一个解决方案。你可以做 nextButton.waitForExists(5000);
这将等待 Next 按钮出现。它将在 5 秒后超时。
关于android - 在我的 uiautomator 测试中,有什么比 sleep 更好的解决方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24471983/