Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java and Espresso - Can't type in, needs to supports input methods or is assignable from class: class SearchView

I'm trying to use Espresso fro testing an application with this xml:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/tvLogin"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:hint="myHint"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:imeOptions="flagNoExtractUi|actionNext"
        android:inputType="text" />

</com.google.android.material.textfield.TextInputLayout>

And this is the test I tried to run:

onView(withId(R.id.tvLogin)).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
onView(withId(R.id.tvLogin)).perform(typeText("test"));

I get this error:

Caused by: java.lang.RuntimeException: Action will not be performed because the target view does not match one or more of the following constraints:
((is displayed on the screen to the user) and (supports input methods or is assignable from class: class android.widget.SearchView))
Target view: "TextInputLayout{id=2131362315, res-name=tvLogin, visibility=VISIBLE, width=831, height=144, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=androidx.constraintlayout.widget.ConstraintLayout$LayoutParams@8241b55, tag=null, root-is-layout-requested=false, has-input-connection=false, x=39.0, y=39.0, child-count=2}"
like image 727
arksdf Avatar asked Nov 27 '25 10:11

arksdf


1 Answers

Your tvLogin is a TextInputLayout, which is also a LinearLayout, and so that's why you get the error when you perform typeText() on it. Since typeText() only works on editable field, and TextInputEditText is a descendant of tvLogin, you can rewrite the test to use one of the convenient matcher ViewMatchers.supportInputMethods() from Espresso:

onView(allOf(supportsInputMethods(), isDescendantOfA(withId(R.id.tvLogin))))
    .perform(typeText("test"))

Alternatively, you can provide an id for the TextInputEditText:

<com.google.android.material.textfield.TextInputEditText
    android:id="@+id/tvLoginEditText"
    android:layout_width="match_parent"
    android:layout_height="56dp"
    android:imeOptions="flagNoExtractUi|actionNext"
    android:inputType="text" />

And then perform typeText() on the editable field instead:

onView(withId(R.id.tvLoginEditText))
    .perform(typeText("test"))
like image 170
Aaron Avatar answered Nov 29 '25 00:11

Aaron