Android Espresso:如何匹配盛大的父母?没有给出确切的层次

时间:2016-12-30 07:08:31

标签: android android-espresso

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())) ;

1 个答案:

答案 0 :(得分:22)

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

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