简体   繁体   中英

Making espresso match view inside ViewPager who is not currently displayed

I have the following view:

<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>

This view is inside a ViewPager. I want to verify the content of the view with the id view_monthly_resume_total_receipts. Since the view is inside a ViewPager I did the following:

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

I have a lot of content, it is not guaranteed that the view is being displayed, it can be in the current view being displayed in the ViewPager but offscreen.

I tried adding ids to the TableLayout, LinearLayout or ScrollView and use withParent and check if the parent is being displayed but wasn't successful.

I tried using hasSiblings and look for R.id.view_monthly_resume_accounts that I know for sure that is being displayed since it is the first view, but also did not worked.

So, my question is: how to make espresso find a view that is inside a view that is inside a ViewPager but the view is offscreen?

After some research I found the method isDescendantOfA.

The method withParent looks only for nearest viewGroup. The isDescendantOfA look for all the tree.

In the example from the question, I can add an id to the LinearLayout near to the ScrollView and update the match to the following:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM