Espresso测试 - 未找到TextInputLayout

时间:2017-03-10 08:57:53

标签: android android-espresso android-testing

我为最近建立的登录屏幕进行了espresso测试以进行测试。

我在用户名和密码的EditTexts中填入一个空字符串。之后我按下按钮。预期的行为是在包含EditTexts的TextInputLayouts中的两个EditTexts下面都会出现一条错误消息。

LoginTest代码:

@RunWith(AndroidJUnit4.class)
@LargeTest
public class LoginTest {

    public static final String EMPTY_USERNAME_STRING_TO_BE_TYPED = "";
    public static final String EMPTY_PASSWORD_STRING_TO_BE_TYPED = "";

    public final String EMPTY_USERNAME_STRING_TO_BE_EXPECTED = "Vul aub een gebruikersnaam in";
    public final String EMPTY_PASSWORD_STRING_TO_BE_EXPECTED = "Vul aub een wachtwoord in";

    @Rule
    public ActivityTestRule<LoginActivity> mActivityRule = new ActivityTestRule<>(LoginActivity.class);

    @Test
    public void checkForEmpty() {

        onView(withId(R.id.editTextLogin)).perform(typeText(EMPTY_USERNAME_STRING_TO_BE_TYPED), closeSoftKeyboard());
        onView(withId(R.id.editTextPassword)).perform(typeText(EMPTY_PASSWORD_STRING_TO_BE_TYPED), closeSoftKeyboard());
        onView(withId(R.id.buttonLogin)).perform(click());

        onView(withId(R.id.input_layout_login))
                .check(ViewAssertions.matches(
                        LoginErrorTextMatchers.withErrorText(
                                Matchers.containsString(EMPTY_USERNAME_STRING_TO_BE_EXPECTED)
                        )
                ));
        onView(withId(R.id.input_layout_password))
                .check(ViewAssertions.matches(
                        LoginErrorTextMatchers.withErrorText(
                                Matchers.containsString(EMPTY_PASSWORD_STRING_TO_BE_EXPECTED)
                        )
                ));
    }

}

LoginErrorTextMatchers代码

public final class LoginErrorTextMatchers {

    @NonNull
    public static Matcher<View> withErrorText(final Matcher<String> expectedErrorText) {
        return new TypeSafeMatcher<View>() {
            @Override
            protected boolean matchesSafely(View view) {
                if (!(view instanceof TextInputLayout)) {
                    return false;
                }

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

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

                return expectedErrorText.equals(error.toString());
            }

            @Override
            public void describeTo(Description description) {

            }
        };
    }
}

布局XML:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/content_login"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="100"
    android:focusableInTouchMode="true"
    tools:context="be.assign.apollo.login.LoginActivity">

    <ImageView
        android:id="@+id/imageViewLogo"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="40"
        android:src="@mipmap/ic_logo_assign"
        android:scaleType="fitCenter"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        />

    <RelativeLayout
        android:id="@+id/frame_form"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="60">
        <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_login"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentEnd="true"
            app:errorTextAppearance="@style/error_appearance">
            <EditText
                android:id="@+id/editTextLogin"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="text"
                android:drawableLeft="@drawable/ic_account"
                android:textColor="@color/colorBlue"
                />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/input_layout_login"
            android:layout_marginTop="8dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentEnd="true"
            app:errorTextAppearance="@style/error_appearance">
            <EditText
                android:id="@+id/editTextPassword"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textPassword"
                android:drawableLeft="@drawable/ic_password"
                android:textColor="@color/colorBlue"
                />
        </android.support.design.widget.TextInputLayout>

        <CheckBox
            android:id="@+id/checkbox_credential_option"
            android:layout_below="@+id/input_layout_password"
            android:layout_marginTop="16dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/checkbox_remember_credentials" />

        <Button
            android:id="@+id/buttonLogin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button_login"
            android:textColor="@drawable/text_rounded_blue"
            android:background="@drawable/button_rounded_blue"
            android:layout_alignParentBottom="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentEnd="true"
            />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/frame_loading"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="50"
        android:visibility="gone">
        <ProgressBar
            android:id="@+id/progressBar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:indeterminateDrawable="@drawable/loading_circle"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="@string/loading_text"/>
    </RelativeLayout>

</LinearLayout>

当我运行此测试时,我收到错误:

android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with id: be.assign.apollo:id/input_layout_login

有人可以告诉我为什么这不起作用吗?

0 个答案:

没有答案
相关问题