How can you check for a TextinputLayout error using Espresso

I want to be able to trigger a match with a TextInputLayout view with a fixed error.

onView(withId(R.id.myTextInputLayout)).check(matches(withText('myError'))); 

withTest () does not seem to work with the TextInputLayout error message. Does anyone else know how to do this?

Thank you for your help.

+5
source share
1 answer

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)); } 
+3
source

All Articles