26
onView(allOf(
        withText(activityUnderTest), 
        withParent(withId(R.id.llh_root_record_activity_3_item))))
    .check(matches(anything())) ;

In above code snippet withParent matcher is failing because view id given is not immediate parent but grand parent.

It can be handled as below, but curious to know the trick,specially when you don't want to specify messy hierarchy as used in below code.

onView(allOf(
        withText(activityUnderTest), 
        withParent(withParent(withParent(withId(R.id.llh_root_record_activity_3_item))))))
    .check(matches(anything())) ;
JJD
  • 50,076
  • 60
  • 203
  • 339

1 Answers1

48

isDescendantOfA is what you need.
Actually, it doesn't apply to grand parent only. Please check espresso cheat sheet

onView(allOf(withText(activityUnderTest), isDescendantOfA(withId(R.id.llh_root_record_activity_3_item))))
            .check(matches(isDisplayed()));
Smeagol
  • 593
  • 9
  • 23
quanlt
  • 834
  • 8
  • 6