Android – how to use espresso to check textinputlayout for errors

I want to be able to run the matcher against a textinputlayout view with an error set

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

Withtest () doesn't seem to apply to textinputlayout error messages. Does anyone else know what to do?

Thank you for your help

resolvent:

Implement a custom viewmatcher to test views that do not support out of the box

The following is an example implementation of the 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));
}

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>