How to find child at some item position in espresso for recyclerview

1.3k views Asked by At

I have been looking over the espresso samples but they only give direct methods to perform click at some position using below method

actionOnItemAtPosition

In most cases we would be needing to click on one of the multiple child on item at some position. Like a TextView, ImageView and Checkbox.

I dont see any simple way to reach to Checkbox at position 25 if my Recyclerview has 50 items. Can anyone has any simple solution for it other than Defining my own ViewAction implementation.

1

There are 1 answers

0
Sammekl On

You can create your own withIndex matcher like this:

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;
        }
    };
}

Usage example

int indexToSelect = 4;
onView(allOf(withIndex(withId(R.id.textViewToSelect), indexToSelect), isDescendantOfA(withId(R.id.customRecyclerView)))).perform(click());