Implement a custom ViewMatcher to check for views that are not supported out of the box.
Below is an example implementation of a withError counter 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)); }
source share