4

I'm trying to reach a TextView and an ImageView within an included view with Espresso, but I always get the following error:

No views in hierarchy found matching: with id: ???:id/llMenuBtnSearch
If the target view is not part of the view hierarchy, you may need to use Espresso.onData to load it from one of the following AdapterViews:android.widget.ListView{66d0aa9 V.ED.VC.. ........ 0,0-720,892 #7f090117 app:id/listView}

I don't know what "use Espresso.onData to load it" means.

I tried to find an answer in the following sources:

Espresso select children of included layout

https://developer.android.com/training/testing/espresso/recipes

https://www.programcreek.com/java-api-examples/index.php?api=android.support.test.espresso.matcher.RootMatchers

But, until now, I was unable to find a solution.

My view hierarchy:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusableInTouchMode="true"
    tools:context="com.sentilant.driviantasks.navigation.Navigation.MapMaster"
    android:tag="mapMaster">

    ...

        <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

            ...

            <include
            android:id="@+id/vRouteDetails"
            layout="@layout/route_details"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:visibility="invisible" />

    </android.support.design.widget.CoordinatorLayout>

</FrameLayout>

And the included layout (included with id:vRouteDetails in the parent layout)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="20dp"
    android:background="@drawable/layout_background"
    android:orientation="vertical">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp">

        <ImageButton
            android:id="@+id/ibRDetailsClose"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="@null"
            app:srcCompat="@drawable/menu_ic_back"
            tools:ignore="ContentDescription" />

        <TextView
            android:id="@+id/tvRDetailsTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/route_details_title"
            android:textSize="18sp"
            tools:text="Route Details" />

    </android.support.design.widget.CoordinatorLayout>
</LinearLayout>

I'm trying to reach both the ImageButton and the TextView. Like so:

onView(allOf(withId(R.id.ibRDetailsClose), withParent(withId(R.id.vRouteDetails))))
                .perform(click());

Is there a solution I missed?

Thank you in advance.

Luís Henriques
  • 604
  • 1
  • 10
  • 30

1 Answers1

4

You can reach your 2 views easily as below:

//check your text view is shown
onView(withId(R.id.tvRDetailsTitle)).check(matches(isDisplayed()));
//click on your image view
onView(withId(R.id.ibRDetailsClose)).perform(click());
Anatolii
  • 14,139
  • 4
  • 35
  • 65
  • Seriously? Wow. How silly of me. I obviously tried that first, but for some reason it didn't work, so I discarded it. Thank you. – Luís Henriques Jun 12 '18 at 11:55
  • 1
    Yep, it's so easy if everything goes according to the plan. However, if there are some async operations before views are shown then it can get more difficult :) – Anatolii Jun 12 '18 at 12:01