Android Espresso. How to check ErrorText in TextInputLayout

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

    You could write a Custom Matcher:

    public final class CustomItemMatchers {
    
    private static class TextInputLayoutErrorMatcher extends BoundedMatcher {
    
      private final Matcher itemTextMatcher;
    
      public TextInputLayoutErrorMatcher(final Matcher 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 withErrorName(final Matcher 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.

    提交回复
    热议问题