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

问题描述 投票:0回答:2

我为我的应用程序编写了一个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。

怎么了?

java android android-espresso
2个回答
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());

0
投票

如果没有错误,则可以安全地假设点击成功。尝试启用“显示点击”开发人员选项以查看点击发生的确切位置。

© www.soinside.com 2019 - 2024. All rights reserved.