Issue
I want to be able to run a matcher against a TextInputLayout view that has an error set.
onView(withId(R.id.myTextInputLayout)).check(matches(withText('myError')));
withTest() does not seem to work with the TextInputLayout error message. Does any else know how to do this?
Thank you for your help.
Solution
Implement a custom ViewMatcher to test views that are not supported out of the box.
Here is an example implementation of withError matcher for TextInputLayout
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));
}
Answered By - Be_Negative
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.