在当前未显示的 ViewPager 中制作浓缩咖啡匹配视图

Making espresso match view inside ViewPager who is not currently displayed

我有以下看法:

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_margin="@dimen/default_spacing">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/view_monthly_resume_accounts"
            android:layout_width="match_parent"
            android:layout_height="100dp"/>

        <!-- a lot of content -->

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="48dp">

            <TableRow>
                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="@string/total_receipts"
                    android:textColor="@color/color_primary_text"/>
                <TextView
                    android:id="@+id/view_monthly_resume_total_receipts"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@color/value_unreceived"
                    android:gravity="end"/>
            </TableRow>
            <!-- more content -->
        </TableLayout>

    </LinearLayout>
</ScrollView>

此视图位于 ViewPager 中。我想验证id为view_monthly_resume_total_receipts的view的内容。由于视图位于 ViewPager 中,因此我执行了以下操作:

onView(allOf(
            withId(R.id.view_monthly_resume_balance),
            isDisplayed()))
            .check(matches(withText(expectedBalance)));

我有很多内容,不保证视图正在显示,可以在ViewPager中显示的当前视图中,但不在屏幕上。

我尝试将 id 添加到 TableLayout、LinearLayout 或 ScrollView 并使用 withParent 并检查父项是否正在显示但没有成功。

我尝试使用 hasSiblings 并寻找 R.id.view_monthly_resume_accounts,我确定它正在显示,因为它是第一个视图,但也没有用。

所以,我的问题是:如何让 espresso 找到位于 ViewPager 内部但视图不在屏幕内的视图?

经过一些研究,我找到了方法 isDescendantOfA。

withParent 方法只查找最近的 viewGroup。 isDescendantOfA 查找所有树。

在问题的示例中,我可以将 id 添加到靠近 ScrollView 的 LinearLayout 并将匹配更新为以下内容:

onView(allOf(
        withId(R.id.view_monthly_resume_balance),
        allOf(
            isDescendantOfA(withId(<linear layout id>)),
            isDisplayed()))
        .check(matches(withText(expectedBalance)));