Android Espresso. How to check ErrorText in TextInputLayout

后端 未结 6 1093
迷失自我
迷失自我 2020-12-16 19:29

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

The view is:



        
6条回答
  •  孤城傲影
    2020-12-16 19:58

    This works with a CustomMatcher:

    public static Matcher hasTextInputLayoutErrorText(final String expectedErrorText) {
        return new TypeSafeMatcher() {
    
            @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) {
            }
        };
    }
    

提交回复
热议问题