我正在使用espresso 2.2.1进行Android测试。我运行的测试用例很少,有些测试用例失败,因为浓咖啡运行得太快。元素甚至没有加载显示,这就是测试用例失败的原因。有没有办法减缓这些运行?
答案 0 :(得分:1)
您可以创建特定的匹配器,它将等待具有id的视图或带有文本或其他内容的视图:
public static ViewAction waitViewWithMatcher(Func1<View, Boolean> predicate, final long millis) {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return isRoot();
}
@Override
public String getDescription() {
return "wait for a specific during " + millis + " millis.";
}
@Override
public void perform(final UiController uiController, final View view) {
uiController.loopMainThreadUntilIdle();
final long startTime = System.currentTimeMillis();
final long endTime = startTime + millis;
do {
for (View child : TreeIterables.breadthFirstViewTraversal(view)) {
if (predicate.call(child)) {
return;
}
}
uiController.loopMainThreadForAtLeast(50);
}
while (System.currentTimeMillis() < endTime);
throw new PerformException.Builder()
.withActionDescription(this.getDescription())
.withViewDescription(HumanReadables.describe(view))
.withCause(new TimeoutException())
.build();
}
};
}
然后你就可以使用它:
onView(isRoot()).perform(EspressoTestUtil.waitViewWithMatcher(v -> withId(R.id.view_id).matches(v), 30000));
等待标识view_id
,30000毫秒的视图。