RecyclerView espresso test click() not working

Issue

I faced with strange espresso instrumentation test behavior. Clicking on recycler view’s item not working.

Click not happened here:

onView(withId(R.id.recyclerView)).perform(actionOnItemAtPosition(0, click()));

But this workaround works:

onView(withId(R.id.recyclerView)).perform(actionOnItemAtPosition(0, recyclerClick()));

// ...

public static ViewAction recyclerClick() {
    return new ViewAction() {

        @Override
        public Matcher<View> getConstraints() {
            return any(View.class);
        }

        @Override
        public String getDescription() {
            return "performing click() on recycler view item";
        }

        @Override
        public void perform(UiController uiController, View view) {
            view.performClick();
        }
    };
}

Is this Espresso or RecyclerView issue?

Solution

Should be nothing to do with RecyclerView specifically. What Espresso does with its ViewActions.click() implementation is sending the MotionEvent to the center of the target view. Make sure no child intercepts it.

Answered By – riwnodennyk

Leave a Comment