Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to click on an item inside a RecyclerView in Espresso

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.

like image 990
Jacki Avatar asked Dec 10 '14 08:12

Jacki


People also ask

Can we use RecyclerView inside RecyclerView in Android?

The RecyclerView widget manages the display and handling of items in a list. It provides Layout Managers to position these items. This way, you can create customized layout managers for RecyclerView containers. We can use a RecyclerView inside another RecyclerView.


2 Answers

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' } 
like image 56
Gabor Avatar answered Sep 19 '22 06:09

Gabor


Just to add to Gabor's answer (which is the correct and complete answer since Espresso 2.0).

You may encounter problems at the moment when using espresso-contrib and RecyclerViews (see android-test-kit ticket).

A workaround is to add this exclusion in the espresso-contrib dependency Gabor mentioned above:

androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.0') {     exclude group: 'com.android.support', module: 'appcompat'     exclude group: 'com.android.support', module: 'support-v4'     exclude module: 'recyclerview-v7' } 

(This is an answer instead of a comment to Gabor's answer because I don't have the right to post comments yet)

like image 37
Sir4ur0n Avatar answered Sep 19 '22 06:09

Sir4ur0n