使用带有自定义 EditText 的 espresso [英] Use espresso with custom EditText
问题描述
这是我的布局的一部分:
Here is part of my layout:
<com.rey.material.widget.EditText
android:id="@+id/tagEditorSettings"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginStart="10dp"
android:layout_toEndOf="@+id/circularProgressbar"
android:hint="@string/tag_name_tag_settings"
android:inputType="text"
android:textSize="18sp"
app:et_dividerColor="@color/my_primary"
app:et_dividerHeight="1dp"
app:et_inputId="@+id/name_input"
app:et_labelEnable="true"
app:et_labelTextSize="14sp"
app:et_supportLines="1" />
和我的测试代码:
onView(withId(R.id.tagEditorSettings))
.perform(click()).perform(clearText());
当我尝试运行它时,我得到这样的错误:
And when I try to run it I get such error:
Caused by: java.lang.RuntimeException: Action will not perform因为目标视图与以下一项或多项不匹配约束条件:(在屏幕上显示给用户并且是可分配的来自类:类 android.widget.EditText)
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 is assignable from class: class android.widget.EditText)
据我所知,问题在于可从类中分配:class android.widget.EditText",但有人可以请教,我如何修复它并在我的情况下使用?
As I understand the problem is with "is assignable from class: class android.widget.EditText", but could someone please advice, how I can fix it and use in my case?
推荐答案
问题:com.rey.material.widget.EditText 扩展自 Framelayout
和 clearText 使用 ReplaceTextAction 为
Issue: com.rey.material.widget.EditText is extended from Framelayout
and clearText uses ReplaceTextAction as
public static ViewAction clearText() {
return actionWithAssertions(new ReplaceTextAction(""));
}
其中 ReplaceTextAction
强制执行 view
,一种 EditText
as
where ReplaceTextAction
enforces the view
, a type of EditText
as
allOf(isDisplayed(), isAssignableFrom(EditText.class));
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
由于 rey...EdiText
不是 EditText
的子类,因此错误
Since rey...EdiText
is not a subclass of EditText
hence the error
解决方案:创建您自己的ViewAction
作为
public static ViewAction clearTextInCustomView(){
return new ViewAction() {
@SuppressWarnings("unchecked")
@Override
public Matcher<View> getConstraints() {
return allOf(isDisplayed(), isAssignableFrom(com.rey.material.widget.EditText.class));
// ^^^^^^^^^^^^^^^^^^^
// To check that the found view is type of com.rey.material.widget.EditText or it's subclass
}
@Override
public void perform(UiController uiController, View view) {
((com.rey.material.widget.EditText) view).setText("");
}
@Override
public String getDescription() {
return "clear text";
}
};
}
然后你可以做
onView(withId(R.id.tagEditorSettings))
.perform(click()).perform(clearTextInCustomView());
这篇关于使用带有自定义 EditText 的 espresso的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!