Using Espresso to click view inside RecyclerView item

How can I use Espresso to click a specific view inside a RecyclerView item? I know I can click the item at position 0 using:

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

But I need to click on a specific view inside that item and not on the item itself.

Thanks in advance.

-- edit --

To be more precise: I have a RecyclerView (R.id.recycler_view) which items are CardView (R.id.card_view). Inside each CardView I have four buttons (amongst other things) and I want to click on a specific button (R.id.bt_deliver).

I would like to use the new features of Espresso 2.0, but I'm not sure that is possible.

If not possible, I wanna use something like this (using Thomas Keller code):

onRecyclerItemView(R.id.card_view, ???, withId(R.id.bt_deliver)).perform(click());

but I don't know what to put on the question marks.


You can do it with customize view action.

public class MyViewAction {

    public static ViewAction clickChildViewWithId(final int id) {
        return new ViewAction() {
            @Override
            public Matcher<View> getConstraints() {
                return null;
            }

            @Override
            public String getDescription() {
                return "Click on a child view with specified id.";
            }

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

}

Then you can click it with

onView(withId(R.id.rv_conference_list)).perform(
            RecyclerViewActions.actionOnItemAtPosition(0, MyViewAction.clickChildViewWithId(R.id. bt_deliver)));

Now with android.support.test.espresso.contrib it has become easier:

1)Add test dependency

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'
}

*exclude 3 modules, because very likely you already have it

2) Then do something like

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

Or

onView(withId(R.id.recyclerView))
  .perform(RecyclerViewActions.actionOnItem(
            hasDescendant(withText("whatever")), click()));

Or

onView(withId(R.id.recycler_linear))
            .check(matches(hasDescendant(withText("whatever"))));

Here is, how I resolved issue in kotlin:

fun clickOnViewChild(viewId: Int) = object : ViewAction {
    override fun getConstraints() = null

    override fun getDescription() = "Click on a child view with specified id."

    override fun perform(uiController: UiController, view: View) = click().perform(uiController, view.findViewById<View>(viewId))
}

and then

onView(withId(R.id.recyclerView)).perform(RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(position, clickOnViewChild(R.id.viewToClickInTheRow)))

You can click on 3rd item of recyclerView Like this:

onView(withId(R.id.recyclerView)).perform(
                RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(2,click()))

Do not forget to provide the ViewHolder type so that inference does not fail.