【发布时间】:2015-03-24 18:49:57
【问题描述】:
场景是当用户点击“下载”按钮时,数据(音乐/图像等)开始从互联网下载。下载完成后,按钮将其标签文本更改为“打开”。然后用户点击那个“打开”按钮。到目前为止我所做的是:
onView(allOf(withId(R.id.button),withText("Download"))).check(matches(isClickable())).perform(click());
try {
Thread.sleep(delayedTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
onView(allOf(withId(R.id.button),withText("Open"))).check(matches(isClickable())).perform(click());
有时测试通过,有时不通过。有时失败的原因是"No views in hierarchy found matching: (with id: something:id/button and with text: is "Open").
而且我知道在 Espresso 中使用 Thread.sleep 也是不好的做法。我已经阅读了有关 Espresso's Idling Resource 的信息,但这对我来说没有意义,我不知道如何在这种特殊情况下应用它。
【问题讨论】:
-
对于 ThreadPools,尝试使用这个:luongbui.com/thread-pool-idlingresource-for-espresso 我已经放了一个完整的例子来展示如何使用我的 IdlingResource,只需在测试代码中注册和取消注册它。请注意,您需要在生产代码中使用的同一个 ThreadPool 实例。强烈建议使用工厂模式或 Dagger。
标签: android android-testing android-espresso