Elye Elye - 1 year ago 273
Android Question

How to test TextInputLayout values (hint, error, etc.) using Android Espresso?

I am trying to test using Espresso if my

views have specific hint. I'd used a code as below:


This works fine for the normal
views, not wrapped in
. However when it wraps around, it no longer works.

I tried to use solution from Android Espresso - How to check EditText hint?, but it still does not working.

I also looked into: https://code.google.com/p/android/issues/detail?id=191261 that reported the issue, it says the workaround is quite easy by pointing to the current
code, but I can't get it to work.

Any ideas to fix this issue?

Answer Source

Here's my custom matcher:

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

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

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

                if (error == null) {
                    return false;

                String hint = error.toString();

                return expectedErrorText.equals(hint);

            public void describeTo(Description description) {

and here's how to use:

public class MainActivityTest {

    public ActivityTestRule<MainActivity> mRule = new ActivityTestRule<>(MainActivity.class);

    public void testMyApp() {


If you would like to check errorText of TextInputLayout, change this line:

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


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

Hope it will help

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download