8

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.

brwngrldev
  • 3,664
  • 3
  • 24
  • 45

2 Answers2

15

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.

brwngrldev
  • 3,664
  • 3
  • 24
  • 45
  • 1
    How would you test click on the item inside the 2nd recyclerview? – Parag Pawar Aug 05 '20 at 09:30
  • 1
    As mentioned here https://stackoverflow.com/questions/62750213/how-to-test-nested-recyclerview-recyclerview-inside-recyclerview-with-espresso this code only works with 0 value for atPosition. – Martin H. Mar 23 '21 at 11:31
4

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

okmanideep
  • 960
  • 7
  • 23