如何使用 Espresso 检查 TextinputLayout 的错误
How can you check a TextinputLayout's error with Espresso
我希望能够 运行 匹配器针对设置了错误的 TextInputLayout 视图。
onView(withId(R.id.myTextInputLayout)).check(matches(withText('myError')));
withTest() 似乎无法处理 TextInputLayout 错误消息。还有其他人知道该怎么做吗?
感谢您的帮助。
实施自定义 ViewMatcher 以测试不支持开箱即用的视图。
这是 TextInputLayout
的 withError 匹配器的示例实现
public static Matcher<View> withErrorInInputLayout(final Matcher<String> stringMatcher) {
checkNotNull(stringMatcher);
return new BoundedMatcher<View, TextInputLayout>(TextInputLayout.class) {
String actualError = "";
@Override
public void describeTo(Description description) {
description.appendText("with error: ");
stringMatcher.describeTo(description);
description.appendText("But got: " + actualText);
}
@Override
public boolean matchesSafely(TextInputLayout textInputLayout) {
CharSequence error = textInputLayout.getError();
if (error != null) {
actualError = error.toString();
return stringMatcher.matches(actualError);
}
return false;
}
};
}
public static Matcher<View> withErrorInInputLayout(final String string) {
return withErrorInInputLayout(is(string));
}
我希望能够 运行 匹配器针对设置了错误的 TextInputLayout 视图。
onView(withId(R.id.myTextInputLayout)).check(matches(withText('myError')));
withTest() 似乎无法处理 TextInputLayout 错误消息。还有其他人知道该怎么做吗?
感谢您的帮助。
实施自定义 ViewMatcher 以测试不支持开箱即用的视图。
这是 TextInputLayout
的 withError 匹配器的示例实现 public static Matcher<View> withErrorInInputLayout(final Matcher<String> stringMatcher) {
checkNotNull(stringMatcher);
return new BoundedMatcher<View, TextInputLayout>(TextInputLayout.class) {
String actualError = "";
@Override
public void describeTo(Description description) {
description.appendText("with error: ");
stringMatcher.describeTo(description);
description.appendText("But got: " + actualText);
}
@Override
public boolean matchesSafely(TextInputLayout textInputLayout) {
CharSequence error = textInputLayout.getError();
if (error != null) {
actualError = error.toString();
return stringMatcher.matches(actualError);
}
return false;
}
};
}
public static Matcher<View> withErrorInInputLayout(final String string) {
return withErrorInInputLayout(is(string));
}