代码之家  ›  专栏  ›  技术社区  ›  Luís Henriques

测试包括使用浓缩咖啡的视图

  •  1
  • Luís Henriques  · 技术社区  · 7 年前

    我正在尝试使用浓缩咖啡访问包含视图中的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 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:vroutedestails)

    <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 回复  |  直到 7 年前
        1
  •  1
  •   Anatolii    7 年前

    您可以很容易地到达您的两个视图,如下所示:

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