android - 在我的 uiautomator 测试中,有什么比 sleep 更好的解决方法?

标签 android android-uiautomator

这是我从 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/

相关文章:

Android 4.2.2 'Not enough space on device' 无法安装应用

android - 如果在 Activity 的 onCreate 中启动长时间运行的任务,Espresso 空闲资源无法工作?

android - UiAutomator 触发事件两次(如 onPressKeycode、onPressEnter)?

android - UiDevice dumpWindowHierarchy 在哪里创建文件?

android - 同时使用 layout-land 和 onConfigurationChanged()

android - 在方向更改时保留列表 fragment 中的列表

android - 是否可以在 uiautomator 中滑动打开/关闭抽屉导航

android - UiAutomator -- 将小部件添加到主屏幕

android - 来自 ThreadPool 线程的 SendBroadcast 阻塞调用

Android Activity 返回堆栈导航问题