英文:
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:
<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}"
答案1
得分: 6
Your tvLogin
是一个TextInputLayout
,它也是一个LinearLayout
,所以当您对其执行typeText()
操作时会出错。由于typeText()
仅适用于可编辑字段,并且TextInputEditText
是tvLogin
的子类,您可以重写测试以使用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("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"))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论