Android Espresso.如何在TextInputLayout中检查ErrorText

Rog*_*ris 10 android android-espresso

基本上我试图测试,登录不正确后,我在电子邮件字段中显示错误.

该观点是:

<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>
Run Code Online (Sandbox Code Playgroud)

我尝试这样做:

onView(withId(R.id.et_email))
    .check(matches(hasErrorText(
        ctx.getString(R.string.authentication_error_empty_email))));
Run Code Online (Sandbox Code Playgroud)

Rog*_*ris 18

这适用于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) {
        }
    };
}
Run Code Online (Sandbox Code Playgroud)

  • 无法弄清楚为什么Espresso`hasErrorText`匹配器不起作用,但这个自定义匹配器工作得很好 (3认同)

her*_*hes 6

我认为您想在TextInputLayout而不是EditText上设置错误。如果正确的话,您可以通过以下方式实现这一目标。

 onView(withId(R.id.ti_email)).check(matches(hasDescendant(
    withText(ctx.getString(R.string.authentication_error_empty_email))))
 )
Run Code Online (Sandbox Code Playgroud)