对于某些视图,perform(click())在espresso ui测试中不起作用

时间:2019-03-05 10:22:27

标签: java android android-espresso

我为我的应用程序编写了一个espresso UI测试,并且对于某些视图(在这种情况下为ImageButton)perform(click())不起作用,也没有显示错误。 这是行不通的:

   try {
        Thread.sleep(TIME_NORMAL_ELPASED);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
onView(allOf(withId(R.id.toolbar_navigation_btn)
            , isCompletelyDisplayed())).perform(click());

这是我的xml:

<RelativeLayout 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"
tools:context="activities.DepartFlight.fragments.AvailableFragment">

<android.support.v7.widget.Toolbar
    android:id="@+id/available_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:layout_alignParentTop="true"
    android:background="@drawable/background_toolbar">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageButton
            android:id="@+id/toolbar_navigation_btn"
            android:layout_width="@dimen/icon_toolbar"
            android:layout_height="@dimen/icon_toolbar"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:background="@drawable/button_selector"
            android:rotation="180"
            android:src="@drawable/ic_back"
            android:tint="@color/white" />

        <com.faranegar.bookflight.customView.CustomTextView
            android:id="@+id/text_toolbar_flight"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="2dp"
            android:textColor="@color/white" />

        <com.faranegar.bookflight.customView.CustomTextView
            android:id="@+id/toolbar_subtitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/text_toolbar_flight"
            android:layout_centerHorizontal="true"
            android:textColor="@color/white"
            android:textSize="@dimen/font_size" />

    </RelativeLayout>
</android.support.v7.widget.Toolbar>

<include
    android:id="@+id/zero_filtered_items"
    android:visibility="gone"
    layout="@layout/zero_filterd_itesm_row" />

<com.faranegar.bookflight.customView.CustomRecyclerView
    android:id="@+id/available_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/depart_flight_activity_footer"
    android:layout_below="@id/available_toolbar"
    android:background="@android:color/transparent"
    android:visibility="gone"
    />

<com.tuyenmonkey.mkloader.MKLoader
    android:id="@+id/progressBar"
    android:layout_width="55dp"
    android:layout_height="55dp"
    android:layout_centerInParent="true"
    android:visibility="gone"
    app:mk_color="@color/colorPrimary"
    app:mk_type="TwinFishesSpinner" />

<RelativeLayout
    android:id="@+id/depart_flight_activity_footer"
    android:layout_width="match_parent"
    android:layout_height="44dp"
    android:layout_alignParentBottom="true"
    android:background="@color/colorPrimary"
    android:visibility="visible">

    <LinearLayout
        android:id="@+id/view_sort"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="18dp"
        android:layout_marginStart="18dp"
        android:gravity="center"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="15dp"
            android:layout_height="15dp"
            android:src="@drawable/sort_icon"
            android:tint="@color/white" />

        <com.faranegar.bookflight.customView.CustomTextView
            android:id="@+id/sortText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginStart="5dp"
            android:text="@string/sort_text"
            android:textColor="@color/white"
            android:textSize="@dimen/text_less_normal" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/view_filter"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="18dp"
        android:layout_toRightOf="@id/view_sort"
        android:gravity="center"
        android:orientation="horizontal"
        android:visibility="visible">

        <ImageView
            android:layout_width="15dp"
            android:layout_height="15dp"
            android:src="@drawable/ic_filter"
            android:tint="@color/white" />

        <com.faranegar.bookflight.customView.CustomTextView
            android:id="@+id/filterText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginStart="5dp"
            android:text="@string/filter_text"
            android:textColor="@color/white"
            android:textSize="@dimen/text_less_normal" />

    </LinearLayout>

    <RelativeLayout
        android:id="@+id/next_day"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/back_day">

        <Button
            android:id="@+id/next_day_available_btn"
            android:layout_width="20dp"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:background="@drawable/button_selector"
            android:textColor="@color/white"
            android:textSize="@dimen/icon_btn" />

        <com.faranegar.bookflight.customView.CustomTextView
            android:id="@+id/next_day_available_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@id/next_day_available_btn"
            android:text="@string/next_day"
            android:textColor="@color/white"
            android:visibility="visible" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/back_day"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="15dp"
        android:layout_marginStart="15dp">

        <Button
            android:id="@+id/prev_day_available_btn"
            android:layout_width="20dp"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/prev_day_available_text"
            android:background="@drawable/button_selector"
            android:textColor="@color/white"
            android:textSize="@dimen/icon_btn" />

        <com.faranegar.bookflight.customView.CustomTextView
            android:id="@+id/prev_day_available_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:text="@string/back_day"
            android:textColor="@color/white" />
    </RelativeLayout>

</RelativeLayout>

<com.faranegar.bookflight.customView.CustomTextView
    android:id="@+id/warning"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="@string/not_available_flights"
    android:textColor="@android:color/black"
    android:textSize="16sp"
    android:visibility="gone" />

<com.faranegar.bookflight.customView.CustomTextView
    android:id="@+id/ads_motto_captan"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/progressBar"
    android:layout_centerInParent="true"
    android:padding="15dp"
    android:text="@string/captan_ads_motto"
    android:textColor="@color/colorPrimary"
    android:textSize="16sp"
    android:visibility="gone" />

<WebView
    android:id="@+id/arzanBelitBanner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/depart_flight_activity_footer"
    android:visibility="gone" />


<include
    android:id="@+id/failedLayout"
    layout="@layout/failed_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:visibility="gone"
    />

<include
    android:id="@+id/saatraapProgressBarLayout"
    layout="@layout/saatraap_brogressbar_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true"
    android:visibility="gone" />

和一个片段显示了上面的XML。

怎么了?

2 个答案:

答案 0 :(得分:1)

我认为setRotation可能会混淆click()和坐标计算。在这种情况下,您可以删除android:rotation然后物理旋转图像,或尝试创建自定义的点击操作:

public static ViewAction forceClick() {
    return new ViewAction() {
        @Override public Matcher<View> getConstraints() {
            return allOf(isClickable(), isEnabled());
        }

        @Override public String getDescription() {
            return "force click";
        }

        @Override public void perform(UiController uiController, View view) {
            view.performClick();
            uiController.loopMainThreadUntilIdle();
        }
    };
}

此操作通过调用performClick()来执行对视图的单击,而没有找到其坐标。但是请确保该视图在应用程序中附加了点击侦听器:

onView(withId(R.id.toolbar_navigation_btn)).perform(forceClick());

答案 1 :(得分:0)

如果没有错误,则可以确定点击成功。尝试启用“显示拍子”开发者选项,以查看点击发生的确切位置。