你可以這樣創建自己的withIndex
匹配:
public static Matcher<View> withIndex(final Matcher<View> matcher, final int index) {
return new TypeSafeMatcher<View>() {
int currentIndex = 0;
@Override
public void describeTo(Description description) {
description.appendText("with index: ");
description.appendValue(index);
matcher.describeTo(description);
}
@Override
public boolean matchesSafely(View view) {
return matcher.matches(view) && currentIndex++ == index;
}
};
}
使用例如
int indexToSelect = 4;
onView(allOf(withIndex(withId(R.id.textViewToSelect), indexToSelect), isDescendantOfA(withId(R.id.customRecyclerView)))).perform(click());