8

मैं एस्प्रेसो का उपयोग करके परीक्षण करने की कोशिश कर रहा हूं यदि मेरे TextInputLayout विचारों का विशिष्ट संकेत है।Android Espresso का उपयोग कर TextInputLayout मानों (संकेत, त्रुटि, आदि) का परीक्षण कैसे करें?

Espresso.onView(ViewMatchers.withId(R.id.edit_text_email)) 
    .check(ViewAssertions.matches(
     ViewMatchers.withHint(R.string.edit_text_email_hint))) 

यह सामान्य EditText विचारों के लिए ठीक काम करता है, TextInputLayout में लिपटे नहीं: मैं नीचे के रूप में एक कोड का इस्तेमाल किया था। हालांकि जब यह चारों ओर लपेटता है, यह अब काम नहीं करता है।

मैंने Android Espresso - How to check EditText hint? से समाधान का उपयोग करने की कोशिश की, लेकिन यह अभी भी काम नहीं कर रहा है।

मैंने यह भी देखा: https://code.google.com/p/android/issues/detail?id=191261 जो इस मुद्दे की रिपोर्ट करता है, यह कहता है कि वर्तमान withHint कोड पर इंगित करके वर्कअराउंड काफी आसान है, लेकिन मैं इसे काम नहीं कर सकता।

इस मुद्दे को ठीक करने के लिए कोई विचार?

उत्तर

16

यहाँ मेरी कस्टम मिलान है:

public static Matcher<View> hasTextInputLayoutHintText(final String expectedErrorText) { 
     return new TypeSafeMatcher<View>() { 

      @Override 
      public boolean matchesSafely(View view) { 
       if (!(view instanceof TextInputLayout)) { 
        return false; 
       } 

       CharSequence error = ((TextInputLayout) view).getHint(); 

       if (error == null) { 
        return false; 
       } 

       String hint = error.toString(); 

       return expectedErrorText.equals(hint); 
      } 

      @Override 
      public void describeTo(Description description) { 
      } 
     }; 
    } 
} 

और यहाँ कैसे उपयोग करने के लिए है:

@RunWith(AndroidJUnit4.class) 
public class MainActivityTest { 

    @Rule 
    public ActivityTestRule<MainActivity> mRule = new ActivityTestRule<>(MainActivity.class); 

    @Test 
    public void testMyApp() { 
     onView(withId(R.id.textInputLayout)).check 
       (matches(hasTextInputLayoutErrorText(mRule.getActivity().getString(R.string 
         .app_name)))); 

    } 

आप TextInputLayout की errorText जाँच करने के लिए चाहते हैं, तो बदल इस लाइन:

 CharSequence error = ((TextInputLayout) view).getHint(); 

के साथ
 CharSequence error = ((TextInputLayout) view).getError(); 

आशा है कि यह

+0

यह काम करता है यहाँ मेरी समाधान है। आपको बहुत - बहुत धन्यवाद! – Elye

+0

जहां मैं कस्टम मैचर रख सकता हूंTextInputLayoutHintText है? – aleksandrbel

+0

चूंकि मेरे कोड में मैंने त्रुटि को TextInputEditText में सेट किया है, इसलिए मुझे MatIn में TextInputEditText में TextInputLayout को बदलना पड़ा। –

1

अधिक सामान्य समाधान है कि है कि "getHint" विधि किसी भी दृश्य के साथ काम करेगा मदद मिलेगी:

public static Matcher<View> withCustomHint(final Matcher<String> stringMatcher) { 
    return new BaseMatcher<View>() { 
     @Override 
     public void describeTo(Description description) { 
     } 

     @Override 
     public boolean matches(Object item) { 
      try { 
       Method method = item.getClass().getMethod("getHint"); 
       return stringMatcher.matches(method.invoke(item)); 
      } catch (NoSuchMethodException e) { 
      } catch (InvocationTargetException e) { 
      } catch (IllegalAccessException e) { 
      } 
      return false; 
     } 
    }; 
} 

उपयोग:

onView(withId(R.id.SomeLayout)).check(matches(withCustomHint(is("SomeString")))); 
0

didn ऊपर समाधान ' मेरे उपयोग के मामले के लिए काम नहीं करते हैं। मैं TextInputEditText ढूंढना चाहता था और इसमें टेक्स्ट टाइप करना चाहता था।

@VisibleForTesting 
class WithTextInputLayoutHintMatcher @RemoteMsgConstructor 
constructor(@field:RemoteMsgField(order = 0) 
      private val stringMatcher: Matcher<String>) : TypeSafeMatcher<View>() { 

    override fun describeTo(description: Description) { 
     description.appendText("with TextInputLayout hint: ") 
     stringMatcher.describeTo(description) 
    } 

    public override fun matchesSafely(textInputEditText: View): Boolean { 
     if (textInputEditText !is TextInputEditText) return false 

     return stringMatcher.matches((textInputEditText.parent.parent as? TextInputLayout)?.hint) 
    } 
} 

/** 
* Returns a matcher that matches [TextInputEditText] based on it's hint property value. 
* 
* 
* **Note:** View's sugar for `withHint(is("string"))`. 
* 
* @param hintText [String] with the hint text to match 
*/ 
fun withTextInputHint(hintText: String): Matcher<View> { 
    return withTextInputHint(Matchers.`is`(checkNotNull(hintText))) 
} 

/** 
* Returns a matcher that matches a descendant of [TextInputEditText] that is displaying the hint 
* associated with the given resource id. 
* 
* @param resourceId the string resource the text view is expected to have as a hint. 
*/ 
fun withTextInputHint(resourceId: Int): Matcher<View> { 
    return withTextInputHint(getString(resourceId)) 
} 

/** 
* Returns a matcher that matches [TextView]s based on hint property value. 
* 
* 
* **Note:** View's hint property can be `null`, to match against it use ` 
* withHint(nullValue(String.class)` 
* 
* @param stringMatcher [`Matcher 
`](http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/Matcher.html) * of [String] with text to match 
*/ 
fun withTextInputHint(stringMatcher: Matcher<String>): Matcher<View> { 
    return WithTextInputLayoutHintMatcher(checkNotNull(stringMatcher)) 
} 

उपयोग::

onView(withTextInputHint(R.string.hint)).perform(ViewActions.typeText("Type text here"))

संबंधित मुद्दे