Skip to content
Advertisement

How to assert inside a RecyclerView in Espresso?

I am using espresso-contrib to perform actions on a RecyclerView, and it works as it should, ex:

//click on first item
onView(withId(R.id.recycler_view))
    .perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));

and I need to perform assertions on it. Something like this:

onView(withId(R.id.recycler_view))
    .perform(
        RecyclerViewActions.actionOnItemAtPosition(
            0, check(matches(withText("Test Text")))
        )
    );

but, because RecyclerViewActions is, of course, expecting an action, it says wrong 2nd argument type. There’s no RecyclerViewAssertions on espresso-contrib.

Is there any way to perform assertions on a RecyclerView?

Advertisement

Answer

You should check out Danny Roa’s solution Custom RecyclerView Actions And use it like this:

onView(withRecyclerView(R.id.recycler_view)
    .atPositionOnView(1, R.id.ofElementYouWantToCheck))
    .check(matches(withText("Test text")));

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement