2

I am writing Espresso unit testing for Recycler view. In recycler view I have text view which is clickable. I want to perform click event on text view in espresso testing. So how to access that text view?

Newts
  • 1,354
  • 14
  • 23

2 Answers2

2

You can use RecyclerViewActions

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

Include to Gradle:

dependencies {
    androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
    androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.0'
}
Nick Titov
  • 588
  • 5
  • 19
1

Try this, I've tested the same

onView(withRecyclerView(R.id.recyclerView)
      .atPositionOnView(position, R.id.childView))
      .perform(click());

Method Calling

public static RecyclerViewMatcher withRecyclerView(final int recyclerViewId) {
    return new RecyclerViewMatcher(recyclerViewId);
}

RecyclerViewMatcher

import android.content.res.Resources;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;

public class RecyclerViewMatcher {

private final int recyclerViewId;

public RecyclerViewMatcher(int recyclerViewId) {
    this.recyclerViewId = recyclerViewId;
}

public Matcher<View> atPosition(final int position) {
    return atPositionOnView(position, -1);
}

public Matcher<View> atPositionOnView(final int position, final int targetViewId) {
    return new TypeSafeMatcher<View>() {
        Resources resources = null;
        View childView;

        public void describeTo(Description description) {
            String idDescription = Integer.toString(recyclerViewId);
            if (this.resources != null) {
                try {
                    idDescription = this.resources.getResourceName(recyclerViewId);
                } catch (Resources.NotFoundException var4) {
                    idDescription = String.format("%s (resource name not found)", recyclerViewId);
                }
            }
            description.appendText("with id: " + idDescription);
        }

        public boolean matchesSafely(View view) {
            this.resources = view.getResources();
            if (childView == null) {
                RecyclerView recyclerView = (RecyclerView) view.getRootView().findViewById(recyclerViewId);

                if (recyclerView != null && recyclerView.getId() == recyclerViewId) {
                    RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForAdapterPosition(position);

                    if (viewHolder != null) {
                        childView = viewHolder.itemView;
                    }
                } else {
                    return false;
                }
            }
            if (targetViewId == -1) {
                return view == childView;
            } else {
                View targetView = childView.findViewById(targetViewId);
                return view == targetView;
            }
        }
    };
}
Rajeshwar
  • 1,471
  • 1
  • 12
  • 13