【问题标题】:Android Espresso click on ImageButton clicks on wrong positionAndroid Espresso点击ImageButton点击错误位置
【发布时间】:2017-03-06 13:07:25
【问题描述】:

当我尝试单击图像按钮时,我在 android 上进行 espresso UI 测试时遇到问题:

我有以下布局:

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:background="#FFFFFF">

        <ImageButton
                android:id="@+id/parent_task_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#00000000"
                android:rotation="90"
                android:layout_gravity="center_vertical"
                android:src="@drawable/ic_subdirectory_arrow_left_black_24dp"
                android:paddingRight="10dp"
                android:paddingLeft="12dp"/>

        <View
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:background="@color/layout_divider"></View>

        <com.mypackage.ui.TasklistItem
                android:id="@+id/parent_task_item"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>

    </LinearLayout>

在我的应用中是这样的:

在我的浓缩咖啡测试中,我尝试点击其中的按钮(带有箭头的那个):

onView(withId(R.id.parent_task_button)).perform(click());

但点击没有按预期工作。我逐步完成了它(启用了android“显示水龙头”和“指针位置”开发人员设置),结果证明测试没有单击以图像按钮为中心,而是单击此处:

...正好在分隔线上,但不在 ImageButton 的中心。

如果我将 ImageButton 替换为普通按钮,则单击是正确的,并且测试正常! 有谁知道这里的问题是什么或如何解决它?

【问题讨论】:

    标签: android android-espresso


    【解决方案1】:

    我找到了导致问题的行(在 ImageButton 中):

     android:rotation="90"
    

    如果我从图像按钮中删除此旋转,则单击正确位于中间。 所以我的临时解决办法是旋转drawable本身而不是按钮。

    我目前也在研究 android espresso 代码并试图在这里找到错误的代码。 点击坐标的计算似乎是错误的,因为它忽略了旋转,因此计算错误。 如果我发现任何东西,会更新这篇文章。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-04
    • 2014-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多