【问题标题】:How to check that the icon is displayed in the input field in android test如何检查图标是否显示在android测试的输入字段中
【发布时间】:2021-11-16 11:30:16
【问题描述】:

应用程序有一个自定义电子邮件输入字段,我如何检查它是否在 android test (espresso) 中显示可绘制图标

xml:

<com.my.mobile.kit.input.InputGeneral
        android:id="@+id/login_email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="10dp"
        app:kitEndIconDrawable="@drawable/kit_ic_cleanitem_24"
        app:kitEndIconMode="clear_text"
        app:kitEndIconTint="@color/baseDeepGreyOne"
        app:kitLabelText="@string/m_auth_login_email_hint"
        app:kitLabelVisibilityMode="manual"
        app:kitPlaceholderText="@string/m_auth_login_email_hint"
        app:layout_constraintTop_toTopOf="parent" />

【问题讨论】:

  • 你看到我的回答了吗?!

标签: android kotlin android-espresso


【解决方案1】:

您可以尝试使用isDescendantOfA

    onView(
        allOf(
            withId(R.id.kitEndIconDrawable),
            isDescendantOfA(withId(R.id.login_email))
        )
   ).check(ViewAssertions.matches(/* check matches R.drawable.kit_ic_cleanitem_24 */))

kitEndIconDrawable 是自定义视图中 ImageView 的 ID

匹配一个drawable是一个不同的话题,有几种方法,你可以使用标签或者一些检查Bitmap,看看这些问题:

Using Espresso to test drawable changes

How to use espresso to test vector drawables

【讨论】:

    【解决方案2】:

    这是一个自定义 视图,那么您应该为执行 单击定义自定义ViewAction该图标.

    类似这样的:

    object OnCloseItemClick : ViewAction {
        override fun getConstraints(): org.hamcrest.Matcher<View>? {
            return ViewMatchers.isAssignableFrom(InputGeneral::class.java)
        }
    
        override fun getDescription(): String {
            return "whatever"
        }
    
        override fun perform(uiController: UiController?, view: View) {
            val customView: InputGeneral = view as InputGeneral
            customView.close_item_id.performClick()
        }
    }
    

    现在你可以测试了:

       onView(withId(R.id.login_email)).perform(OnCloseItemClick)
    

    【讨论】:

      猜你喜欢
      • 2017-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-23
      • 1970-01-01
      相关资源
      最近更新 更多