安卓 Espresso 。如何检查 TextInputLayout 中的 ErrorText

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

我试着这样做:

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

最佳答案

这适用于 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) {
        }
    };
}

关于安卓 Espresso 。如何检查 TextInputLayout 中的 ErrorText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34285782/

相关文章:

android - SurfaceView 在加载时闪烁黑色

android - Geo Fix 命令不通过高度

android - 如何在 Android 仪器测试中从测试 Assets 访问 File 对象?

Android 编译不同资源(白标)

java - 如何查找并替换字符串中的 '['?

android - Phonegap 版本 : accelerometer not working android

android - "Attempt to read from field ' int android.content.pm.ApplicationInfo.targetSdkVersion ' on a null object reference"异常

android - 咖啡安卓 : Click on Image under sub Linear Layout

android - AndroidX 的未解决引用 ActivityTestRule

Android espresso 在同一个 Activity 中连续测试多个 fragment