How to click on an item inside a RecyclerView in Espresso

Jacki picture Jacki · Dec 10, 2014 · Viewed 33.7k times · Source

I have a RecyclerView (R.id.recyclerView) where each row has an image (R.id.row_image) and a TextView. I want to click on the image in the first row.
I've tried to use onData(..) but it doesn't seem to work.

Answer

Gabor picture Gabor · Dec 27, 2014

Use RecyclerViewActions

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

Include this in your gradle script:

dependencies {
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
    androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.0'
}