Android Espresso : 怎么搭配爷爷奶奶?没有给出确切的层次结构

Android Espresso : How to match grand parent? without giving exact hierarchy

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

在上面的代码片段中 withParent 匹配器失败,因为给定的视图 ID 不是直接父级而是祖父级。

它可以如下处理,但很想知道这个技巧,特别是当您不想指定下面代码中使用的凌乱层次结构时。

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

isDescendantOfA 就是您所需要的。
实际上,它不仅仅适用于 grand parent。 请检查 espresso cheat sheet

onView(allOf(withText(activityUnderTest), isDescendantOfA(withId(R.id.llh_root_record_activity_3_item))))
            .check(matches(isDisplayed()));