Java 和 Espresso – 不能输入,需要支持输入方法或可分配给类:class SearchView

huangapple go评论92阅读模式
英文:

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

问题

我正在尝试使用Espresso来测试一个具有以下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>

这是我尝试运行的测试:

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

我收到了以下错误:

Caused by: java.lang.RuntimeException: 由于目标视图不满足以下一个或多个约束不会执行操作
((在用户屏幕上显示)(支持输入法或可分配给类的条件class android.widget.SearchView))
目标视图"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}"
英文:

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

&lt;com.google.android.material.textfield.TextInputLayout
    android:id=&quot;@+id/tvLogin&quot;
    android:layout_width=&quot;0dp&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:hint=&quot;myHint&quot;
    app:layout_constraintEnd_toEndOf=&quot;parent&quot;
    app:layout_constraintStart_toStartOf=&quot;parent&quot;
    app:layout_constraintTop_toTopOf=&quot;parent&quot;&gt;

    &lt;com.google.android.material.textfield.TextInputEditText
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;56dp&quot;
        android:imeOptions=&quot;flagNoExtractUi|actionNext&quot;
        android:inputType=&quot;text&quot; /&gt;

&lt;/com.google.android.material.textfield.TextInputLayout&gt;

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(&quot;test&quot;));

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: &quot;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}&quot;

答案1

得分: 6

Your tvLogin是一个TextInputLayout,它也是一个LinearLayout,所以当您对其执行typeText()操作时会出错。由于typeText()仅适用于可编辑字段,并且TextInputEditTexttvLogin的子类,您可以重写测试以使用Espresso中的一个便捷匹配器ViewMatchers.supportInputMethods()

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

或者,您可以为TextInputEditText提供一个ID:

<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" />

然后在可编辑字段上执行typeText()

onView(withId(R.id.tvLoginEditText))
    .perform(typeText("test"))

希望对您有所帮助。

英文:

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(&quot;test&quot;))

Alternatively, you can provide an id for the TextInputEditText:

&lt;com.google.android.material.textfield.TextInputEditText
    android:id=&quot;@+id/tvLoginEditText&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;56dp&quot;
    android:imeOptions=&quot;flagNoExtractUi|actionNext&quot;
    android:inputType=&quot;text&quot; /&gt;

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

onView(withId(R.id.tvLoginEditText))
    .perform(typeText(&quot;test&quot;))

huangapple
  • 本文由 发表于 2020年8月2日 03:02:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63209022.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定