在我的android检测测试中,我试图点击页面视图之外的回收站视图。我想滚动到这个元素,然后点击它。
我写了代码
onView(allOf(withId(R.id.offerSummaryLayout), hasDescendant(withText("Online deal")), FirstViewMatcher.firstView())).perform(scrollTo(), click());
然而,当我这样做时,它会抛出此错误
android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: (with id: uk.co.myAPP.android.beta:id/offerSummaryLayout and has descendant: with text: is "Online sale" and is the first view that comes along )
If the target view is not part of the view hierarchy, you may need to use Espresso.onData to load it from one of the following AdapterViews:android.widget.GridView{3395c7d VFED.VC.. .F...... 0,120-768,700 #7f1000b9 app:id/main_categories_grid_view}
是否有其他人对这个问题进行了解决或者有我可以使用的决议
答案 0 :(得分:0)
定义自定义匹配器:
private <View> Matcher<View> first(final Matcher<View> matcher) {
return new BaseMatcher<View>() {
boolean isFirst = true;
@Override
public boolean matches(final Object item) {
if (isFirst && matcher.matches(item)) {
isFirst = false;
return true;
}
return false;
}
@Override
public void describeTo(final Description description) {
description.appendText("should return first matching item");
}
};
}
然后您可以将其用作OnView(allOf(withId(R.id.offerSummaryLayout)).perform(RecyclerViewActions.actionOnItem(first(hasDescendant(withText("Online deal"))),click()));