android expresso 测试两次调用 onview 失败
android expresso testing fail calling onview twice
我正在测试我为 android 编写的代码,我正在使用 JUnit4 和 android.support.test.espresso.Espresso.*
。
如果我在之前调用的 android 对象(如 EditText
、TexView
、Button
上调用两次 onview
,就会发生这种情况,否则,我会得到一个异常,我不会不知道是我遗漏了什么还是错误,如何解决?
onView(withId(R.id.name))
.perform(typeText(NAME), closeSoftKeyboard());
onView(withId(R.id.surname))
.perform(typeText(SURNAME), closeSoftKeyboard());
onView(withId(R.id.name))
.perform(replaceText(NAME), closeSoftKeyboard());
错误日志:
android.support.test.espresso.PerformException: Error performing 'replace text' on view 'with id: com.myapp:id/name'.
at android.support.test.espresso.PerformException$Builder.build(PerformExceptio
n.java:84)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:81)
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:52)
at android.support.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:312)
at android.support.test.espresso.ViewInteraction.desugaredPerform(ViewInteraction.java:167)
at android.support.test.espresso.ViewInteraction.perform(ViewInteraction.java:110)
at com...
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)
布局xml:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="68dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<GridLayout
android:id="@+id/gridLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="2"
android:orientation="horizontal"
android:rowCount="9">
<TextView
android:id="@+id/nameLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/nomeLabel" />
<EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
<TextView
android:id="@+id/surnameLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cognomeLabelVal" />
<EditText
android:id="@+id/surname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
<Button
android:id="@+id/saveId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:onClick="save"
android:text="@string/saveName" />
</GridLayout>
</ScrollView>
根据日志,无法从 EditText
分配视图 name
。请参阅 ViewActions.replaceText()
的文档
您需要为此编写自己的 ViewAction
。
来自另一个 post and this
创建您自己的 ViewAction
,如下所示。
public static ViewAction setTextInTextView(final String value){
return new ViewAction() {
@SuppressWarnings("unchecked")
@Override
public Matcher<View> getConstraints() {
return allOf(isDisplayed(), isAssignableFrom(TextView.class));
// ^^^^^^^^^^^^^^^^^^^
// To check that the found view is TextView or it's subclass like EditText
// so it will work for TextView and it's descendants
}
@Override
public void perform(UiController uiController, View view) {
((TextView) view).setText(value);
}
@Override
public String getDescription() {
return "replace text";
}
};
}
然后像这样替换你的代码
onView(withId(R.id.name))
.perform(setTextInTextView(NAME), closeSoftKeyboard());
onView(withId(R.id.surname))
.perform(setTextInTextView(SURNAME), closeSoftKeyboard());
onView(withId(R.id.name))
.perform(setTextInTextView(NAME), closeSoftKeyboard());
我正在测试我为 android 编写的代码,我正在使用 JUnit4 和 android.support.test.espresso.Espresso.*
。
如果我在之前调用的 android 对象(如 EditText
、TexView
、Button
上调用两次 onview
,就会发生这种情况,否则,我会得到一个异常,我不会不知道是我遗漏了什么还是错误,如何解决?
onView(withId(R.id.name))
.perform(typeText(NAME), closeSoftKeyboard());
onView(withId(R.id.surname))
.perform(typeText(SURNAME), closeSoftKeyboard());
onView(withId(R.id.name))
.perform(replaceText(NAME), closeSoftKeyboard());
错误日志:
android.support.test.espresso.PerformException: Error performing 'replace text' on view 'with id: com.myapp:id/name'.
at android.support.test.espresso.PerformException$Builder.build(PerformExceptio
n.java:84)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:81)
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:52)
at android.support.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:312)
at android.support.test.espresso.ViewInteraction.desugaredPerform(ViewInteraction.java:167)
at android.support.test.espresso.ViewInteraction.perform(ViewInteraction.java:110)
at com...
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)
布局xml:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="68dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<GridLayout
android:id="@+id/gridLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="2"
android:orientation="horizontal"
android:rowCount="9">
<TextView
android:id="@+id/nameLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/nomeLabel" />
<EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
<TextView
android:id="@+id/surnameLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cognomeLabelVal" />
<EditText
android:id="@+id/surname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
<Button
android:id="@+id/saveId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:onClick="save"
android:text="@string/saveName" />
</GridLayout>
</ScrollView>
根据日志,无法从 EditText
分配视图 name
。请参阅 ViewActions.replaceText()
您需要为此编写自己的 ViewAction
。
来自另一个 post ViewAction
,如下所示。
public static ViewAction setTextInTextView(final String value){
return new ViewAction() {
@SuppressWarnings("unchecked")
@Override
public Matcher<View> getConstraints() {
return allOf(isDisplayed(), isAssignableFrom(TextView.class));
// ^^^^^^^^^^^^^^^^^^^
// To check that the found view is TextView or it's subclass like EditText
// so it will work for TextView and it's descendants
}
@Override
public void perform(UiController uiController, View view) {
((TextView) view).setText(value);
}
@Override
public String getDescription() {
return "replace text";
}
};
}
然后像这样替换你的代码
onView(withId(R.id.name))
.perform(setTextInTextView(NAME), closeSoftKeyboard());
onView(withId(R.id.surname))
.perform(setTextInTextView(SURNAME), closeSoftKeyboard());
onView(withId(R.id.name))
.perform(setTextInTextView(NAME), closeSoftKeyboard());