Home > Software engineering >  How to check that the icon is displayed in the input field in android test
How to check that the icon is displayed in the input field in android test

Time:11-16

Input element

The application has a custom email input field, how can I check that it displays the drawable icons in 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" />

CodePudding user response:

You can try using isDescendantOfA

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

Where kitEndIconDrawable is the id of the ImageView inside your custom view

Matching a drawable is a different topic, there are several approaches, you can use tags or some check the Bitmap, see these questions:

Using Espresso to test drawable changes

How to use espresso to test vector drawables

CodePudding user response:

This is a custom view then you should define custom ViewAction for perform click on that icon.

Something like this :

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()
    }
}

Now you can test this :

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