2015-12-15 17 views
5

Về cơ bản tôi đang cố gắng kiểm tra sau khi đăng nhập không đúng, tôi có một lỗi hiển thị trong trường email.Android Espresso. Làm thế nào để kiểm tra ErrorText trong TextInputLayout

Quan điểm là:

<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> 

tôi cố gắng để làm điều đó như thế này:

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

Trả lời

11

này làm việc với một 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) { 
     } 
    }; 
} 
2

Bạn có thể viết một Matcher tùy chỉnh:

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

Bạn có thể sử dụng nó sau đó với

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

Mã này được viết bằng Espresso 1, nhưng tôi hy vọng nó vẫn hoạt động.

-4

Vui lòng sử dụng phương thức setError() của EditText Ví dụ: EditText emailEditText;

if(invalidLogin) 
    emailEditText.setError("Error Message"); 
Các vấn đề liên quan