RogerParis
RogerParis

Reputation: 1559

Android Espresso. How to check ErrorText in TextInputLayout

Basically I am trying to test that after login incorrectly I have an error showing in the email field.

The view is:

<android.support.design.widget.TextInputLayout
    android:id="@+id/ti_email"
    android:paddingEnd="10dp"
    android:paddingTop="10dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingStart="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText
        android:id="@+id/et_email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/authentication_email_placeholder"
        android:inputType="textEmailAddress"
        android:maxLines="1"
        android:textSize="16sp"
        tools:text="@string/placeholder_email"/>

</android.support.design.widget.TextInputLayout>

I try to do it like this:

onView(withId(R.id.et_email))
    .check(matches(hasErrorText(
        ctx.getString(R.string.authentication_error_empty_email))));

Upvotes: 13

Views: 6019

Answers (7)

Stefan Indaco
Stefan Indaco

Reputation: 84

If you don't want to use a custom matcher, in Kotlin you can get the same result by

    val expectedString = ctx.getString(R.string.authentication_error_empty_email)
    onView(ViewMatchers.withId(R.id.ti_email))
        .check { view, _ ->
            val actualError = (view as TextInputLayout).error
            assertEquals(actualError, expectedError)
        }

}

I like this way best because it allows flexibility with any view

Upvotes: 2

heronsanches
heronsanches

Reputation: 596

I think that you would like to set error on TextInputLayout not on EditText. If it is correct, in the following way you could achieve this.

 onView(withId(R.id.ti_email)).check(matches(hasDescendant(
    withText(ctx.getString(R.string.authentication_error_empty_email))))
 )

Upvotes: 6

martinseal1987
martinseal1987

Reputation: 2419

wrote a custom matcher

import android.view.View;

import com.google.android.material.textfield.TextInputLayout;

import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;

public class TextInputLayoutErrorMatcher extends TypeSafeMatcher<View>{

private String expectedErrorText;

TextInputLayoutErrorMatcher(String expectedErrorText) {
    this.expectedErrorText = expectedErrorText;
}

@Override
protected boolean matchesSafely(View item) {

    if (!(item instanceof TextInputLayout)) {
        return false;
    }

    CharSequence error = ((TextInputLayout) item).getError();

    if (error == null) {
        return false;
    }

    String hint = error.toString();

    return expectedErrorText.equals(hint);
}

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

}

Upvotes: 0

Sajid Zeb
Sajid Zeb

Reputation: 1968

Its working :)

onView(withId(R.id.et_email)).check(matches(hasErrorText("Error Message")));

Upvotes: -1

Shiva
Shiva

Reputation: 88

Please use setError() method of EditText Ex : EditText emailEditText;

if(invalidLogin)
    emailEditText.setError("Error Message");

Upvotes: -6

RogerParis
RogerParis

Reputation: 1559

This works with a CustomMatcher:

public static Matcher<View> hasTextInputLayoutErrorText(final String expectedErrorText) {
    return new TypeSafeMatcher<View>() {

        @Override
        public boolean matchesSafely(View view) {
            if (!(view instanceof TextInputLayout)) {
                return false;
            }

            CharSequence error = ((TextInputLayout) view).getError();

            if (error == null) {
                return false;
            }

            String hint = error.toString();

            return expectedErrorText.equals(hint);
        }

        @Override
        public void describeTo(Description description) {
        }
    };
}

Upvotes: 24

Christopher
Christopher

Reputation: 10259

You could write a Custom Matcher:

public final class CustomItemMatchers {

private static class TextInputLayoutErrorMatcher extends BoundedMatcher<Object, Wrapper> {

  private final Matcher<String> itemTextMatcher;

  public TextInputLayoutErrorMatcher(final Matcher<String> itemTextMatcher){
     super(TextInputLayout.class);
     this.itemTextMatcher = itemTextMatcher;
  }

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

  @Override
  protected boolean matchesSafely(TextInputLayout til) {
     if (til == null) {
        return false;
     }
     return itemTextMatcher.matches((til.getError());
  }
}

public static Matcher<Object> withErrorName(final Matcher<String> itemTextMatcher) {
  checkNotNull(itemTextMatcher);
  return new TextInputLayoutErrorMatcher(itemTextMatcher);
}
}

You can use it then with

matches(CustomItemMatchers.withErrorName(equalTo("My Error")))

This code was written with Espresso 1, but I hope it still works.

Upvotes: 4

Related Questions