测试包括使用Espresso的视图

时间:2018-06-12 10:59:14

标签: android layout android-espresso

我尝试使用Espresso在包含的视图中访问TextView和ImageView,但我总是收到以下错误:

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}

我不知道"使用Espresso.onData加载它"装置

我试图在以下来源中找到答案:

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

但是,到现在为止,我无法找到解决方案。

我的视图层次结构:

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

包含的布局(包含在父布局中的id:vRouteDetails)

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

我试图同时访问ImageButton和TextView。 像这样:

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

我错过了解决方案吗?

提前谢谢。

1 个答案:

答案 0 :(得分:1)

您可以轻松地查看2个视图,如下所示:

//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());