Espresso Android 匹配父级中的文本视图

Espresso Android matching textviews inside parent

我有一个简单的线性布局,里面有两个文本视图。线性布局有一个唯一的 id 但 textviews 没有。如何验证其中一个文本来自那些文本视图?

我尝试了以下代码:

 onView(allOf(NavigationDrawerComponent.topSectionWrapper,
                hasSibling(withClassName(Matchers.equalTo(TextView.class.getSimpleName())))))
                .check(matches(withText(Data.fullUserName)));

不幸的是,它不适用于 me.I 我收到以下错误:

   android.support.test.espresso.NoMatchingViewException: No views in
    hierarchy found matching

使用hasDescendant()

onView(withId(R.id.recycler_view)).check(matches(atPosition(0, hasDescendant(withText("Available")))));

有时您可能想滚动 RecyclerView 直到找到正确的子视图(如果该视图不可见,则可能尚未创建)。在这种情况下,使用 scrollTo 是不够的,Android Studio 不会自动推荐更好的选择。你必须这样做:

 onView(withId(R.id.recycler_view))
 .perform(RecyclerViewActions.scrollTo(hasDescendant(withText("Available"))));