在ViewPager中制作当前未显示的espresso匹配视图

时间:2017-03-01 12:49:01

标签: android android-espresso

我有以下观点:

<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验证视图的内容。由于视图位于ViewPager中,因此我执行了以下操作:

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

我有很多内容,无法保证视图正在显示,它可以在当前视图中显示在ViewPager中但在屏幕外。

我尝试将ids添加到TableLayout,LinearLayout或ScrollView并使用withParent并检查父项是否显示但未成功。

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

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

1 个答案:

答案 0 :(得分:0)

经过一番研究后,我发现方法是DescendantOfA。

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)));
相关问题