Android Espresso. How to check ErrorText in TextInputLayout

后端 未结 6 1089
迷失自我
迷失自我 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:55

    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{
    
    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);
    }
    

    }

提交回复
热议问题