Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test if an EditText view has no error text set with Espresso on Android?

I know how to test if an error text is set in an EditText:

editText.check(matches(hasErrorText("")));

Now I want to test if an EditText has no error text set. I've tried this, but it does not work.

editText.check((matches(not(hasErrorText("")))));

Does anyone know how to do that? Thanks!

like image 485
aiueoH Avatar asked Mar 08 '17 10:03

aiueoH


1 Answers

I don't think it's possible that way, depending on what you want exactly, I would use a custom matcher:

public static Matcher<View> hasNoErrorText() {
    return new BoundedMatcher<View, EditText>(EditText.class) {

        @Override
        public void describeTo(Description description) {
            description.appendText("has no error text: ");
        }

        @Override
        protected boolean matchesSafely(EditText view) {
            return view.getError() == null;
        }
    };
}

This matcher can check if an EditText does not have any error text set, use it like this:

onView(allOf(withId(R.id.edittext), isDisplayed())).check(matches(hasNoErrorText()));
like image 198
stamanuel Avatar answered Oct 23 '22 13:10

stamanuel