问题描述
我正在为Android应用创建用户界面测试。该测试点击一个UI元素,并期望出现另一个UI元素。只有在点击第一个UI元素后包含Thread.sleep
时,测试才会通过。如何避免这种情况?
这是测试:
@Test
public void mapInfoIsdisplayed() throws InterruptedException {
// Tap on the crosshair at the centre of the map.
onView(withId(R.id.map_crosshairs)).perform(click());
Thread.sleep(100); // Why is this required?
// Verify that the view is displayed
onView(withId(R.id.feature_title)).check(matches(isCompletelydisplayed()));
}
该应用程序用RxJava编写,使用Hilt进行依赖项注入,并使用Espresso作为UI测试框架。
我希望测试依赖项在主线程上同步运行,因此我的测试使用:
我还通过模拟器设置禁用了动画,并且还向我的build.gradle
中添加了以下内容(尽管我不确定它可以做什么):
testOptions {
animationsdisabled = true
}
有了这些,我希望测试能够同步运行,等待所有相关线程完成后再测试是否显示新视图。这似乎没有发生,所以我要进行片状测试。
我非常怀疑生产代码是在某个地方异步执行的,问题是我无法查明位置(如果Android Studio中有某种方法可以找到它,那就太好了。)
如何确保所有代码同步执行,从而避免使用Thread.sleep
?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)