How to test nested RecyclerView (RecyclerView inside RecyclerView) with Espresso

Issue

I have implemented RecyclerView(childRecyclerView) inside another RecyclerView(parentRecyclerView), picture blow explains all the implentation:

enter image description here

I want to write an espresso test that checks all TextViews inside the childRecyclerView are with the expected text, I have checked this answer https://stackoverflow.com/a/34141230/3522182, so in my case is this:

onView(allOf(isDescendantOfA(withRecyclerView(R.id.parentRecyclerView).atPosition(0)),
                isDescendantOfA(withRecyclerView(R.id.childRecyclerView).atPosition(0)),
                withId(R.id.textView1)))
                .check(matches(withText("some text")));

it’s ok, but the problem I had was that when I pass 1 for atPosition in withRecyclerView(R.id.parentRecyclerView).atPosition(0) I get an error that says the view is not found in the hierarchy:

No views in hierarchy found matching: (is descendant of a: RecyclerView with id: com.example.testapp:id/parentRecyclerView at position: 1

is there another way to test childRecyclerView contents?

Solution

i used this and it works:

onView(withId(R.id.parentRecyclerView))
    .perform(RecyclerViewActions.scrollToPosition<RecyclerView.ViewHolder>(0))

onView(
    allOf(
        isDescendantOfA(allOf(
            withId(R.id.contentRootLayout),
            hasDescendant(withText("title text"))
        )),
        isDescendantOfA(withId(R.id.childRecyclerView)),
        withId(R.id.textView1),
        withText("some text")
    )
).check(matches(isDisplayed()))

Answered By – ali-star

Leave a Comment