Espresso - click a single list view item

Try this.

onData(anything()).inAdapterView(withId(R.id.list_view)).atPosition(0).perform(click());

If you don't know the layout ID of the ListView, you can still run an Espresso test on it:

// Perform a click on the 5th item of a ListView.
// anything() is a hamcrest matcher that always matches.
// It's useful if you don't care what the object under test is.
onData(anything()).atPosition(5).perform(click());

This will work on Dialogs and DialogFragments which are displaying a list of items. And you don't need to specify the ListView ID.

Source: https://github.com/shohrabuddin/Espresso


Note: To quickly add the imports for these methods, put the blinking cursor on the unresolved method, then do Android Studio ➔ HelpFind Action ➔ search for "show context action" or "show intention action" ➔ click on the result option ➔ A popup window will appear ➔ click on "Import static method ...". You can also assign a keyboard shortcut to "Show Context Actions". More info here. Another way is to enable "Add unambiguous imports on the fly" in the Settings.


The beauty of Espresso is in not dealing with all this 'timeout' mess.

Have you disabled animations as mentioned in Espresso documentation:

On your device, under Settings->Developer options disable the following 3 settings:

Window animation scale

Transition animation scale

Animator duration scale

?