brwngrldev
brwngrldev

Reputation: 3704

Espresso Matcher for Nested RecyclerView

So here's a breakdown of my hierarchy:

RecyclerView --> LinearLayout --> RecyclerView --> FrameLayout -> FrameLayout 

Here's a screenshot:

layout

I would like to be able to verify the FrameLayout with the text is displayed. This what I have tried so far:

onView(withRecyclerView(R.id.container_list).atPositionOnView(0, R.id.row_content))
                .check(matches(withRecyclerView(R.id.row_content).atPositionOnView(0, R.id.info_field)))
                .check(matches(isDisplayed()));

But it causes an AmbiguousViewMatcherException. Any ideas on how to verify that nested view? Should mention I'm using the ViewMatcher from here. Thanks.

Upvotes: 8

Views: 4056

Answers (2)

brwngrldev
brwngrldev

Reputation: 3704

I was able to verify it using the explanation @manidesto provided above with some slight changes.

onView(allOf(isDescendantOfA(withRecyclerView(R.id.container_list).atPosition(0)),
                isDescendantOfA(withRecyclerView(R.id.row_content).atPosition(0)),
                withId(R.id.info_field)))
                .check(matches(isDisplayed()));

Main enhancement is I used the allOf matcher to specify multiple characteristics of the view I was trying to verify.

Upvotes: 15

okmanideep
okmanideep

Reputation: 1090

Try this

onView(withRecyclerView(R.id.container_list).atPositionOnView(0, R.id.row_content))
    .check(matches(withChild(withId(R.id.info_field))))

This just checks whether the View(R.id.row_content) has a child with id R.id.info_field

Note:

.check() takes in a ViewAssertion which means that the view matched by your ViewMatcher given to the onView() method is just asserted by the ViewAssertion

So when you do

onView(withRecyclerView(R.id.container_list).atPositionOnView(0, R.id.row_content))
    .check(matches(withChild(withId(R.id.info_field))))
    .check(matches(isDisplayed()))

the ViewMatcher - isDisplayed() is applied to R.id.row_content matched inside onView() not the R.id.info_field asserted to matching in .check() call

Upvotes: 4

Related Questions