Wednesday, 15 July 2015

Android espresso assert EditText error (setError)



This code works:

public void testErrorMessage() {
getActivity();
onView(withId(R.id.edit_text)).check(matches(hasErrorText("My expected error text")));
}

private static Matcher<? super View> hasErrorText(String expectedError) {
return new ErrorTextMatcher(expectedError);
}

private static class ErrorTextMatcher extends TypeSafeMatcher<View> {
private final String expectedError;

private ErrorTextMatcher(String expectedError) {
this.expectedError = checkNotNull(expectedError);
}

@Override
public boolean matchesSafely(View view) {
if (!(view instanceof EditText)) {
return false;
}
EditText editText = (EditText) view;
return expectedError.equals(editText.getError());
}

@Override
public void describeTo(Description description) {
description.appendText("with error: " + expectedError);
}
}







https://groups.google.com/forum/#!topic/android-test-kit-discuss/iIicnjeRrQc

No comments:

Post a comment