2015-12-15 9 views
5

में त्रुटि टेक्स्ट को कैसे जांचें मूल रूप से मैं परीक्षण करने की कोशिश कर रहा हूं कि गलत तरीके से लॉगिन करने के बाद मुझे ईमेल फ़ील्ड में एक त्रुटि दिखाई दे रही है।एंड्रॉइड एस्प्रेसो। TextInputLayout

दृश्य है:

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

मैं इस तरह यह करने के लिए प्रयास करें:

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

उत्तर

11

यह एक 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

आप लिख सकते हैं एक कस्टम Matcher:

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

आप के साथ तो यह उपयोग कर सकते हैं

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

यह कोड एस्प्रेसो 1 के साथ लिखा गया था, लेकिन मुझे उम्मीद है कि यह अभी भी काम करता है।

-4

कृपया EditText पूर्व की setError() विधि का उपयोग करें पूर्व: EditText ईमेलEditText;

if(invalidLogin) 
    emailEditText.setError("Error Message"); 
संबंधित मुद्दे