Android Espresso。如何在TextInputLayout中检查ErrorText

时间:2015-12-15 09:45:58

标签: 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))));

7 个答案:

答案 0 :(得分:16)

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

答案 1 :(得分:3)

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

 onView(withId(R.id.ti_email)).check(matches(hasDescendant(
    withText(ctx.getString(R.string.authentication_error_empty_email))))
 )

答案 2 :(得分:2)

您可以编写自定义匹配器:

row_iter = PyObject_GetIter(seqseq);
if (row_iter == NULL) {
    PyErr_SetString(PyExc_TypeError,
                    "writerows() argument must be iterable");
    return NULL;
}
while ((row_obj = PyIter_Next(row_iter))) {
    result = csv_writerow(self, row_obj);
    Py_DECREF(row_obj);
    if (!result) {
        Py_DECREF(row_iter);
        return NULL;
    }
    else
         Py_DECREF(result);
}
Py_DECREF(row_iter);
if (PyErr_Occurred())
    return NULL;
Py_INCREF(Py_None);
return Py_None;

您可以随后使用

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

此代码是用Espresso 1编写的,但我希望它仍然有效。

答案 3 :(得分:0)

工作正常:)

        onView(withId(R.id.et_email)).check(matches(hasErrorText("Error Message")));

答案 4 :(得分:0)

编写自定义匹配器

import android.view.View;

import com.google.android.material.textfield.TextInputLayout;

import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;

public class TextInputLayoutErrorMatcher extends TypeSafeMatcher<View>{

private String expectedErrorText;

TextInputLayoutErrorMatcher(String expectedErrorText) {
    this.expectedErrorText = expectedErrorText;
}

@Override
protected boolean matchesSafely(View item) {

    if (!(item instanceof TextInputLayout)) {
        return false;
    }

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

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

    String hint = error.toString();

    return expectedErrorText.equals(hint);
}

@Override
public void describeTo(Description description) {
    description.appendText("with error text " + expectedErrorText);
}

}

答案 5 :(得分:0)

如果您不想使用自定义匹配器,在 Kotlin 中您可以通过

获得相同的结果
    val expectedString = ctx.getString(R.string.authentication_error_empty_email)
    onView(ViewMatchers.withId(R.id.ti_email))
        .check { view, _ ->
            val actualError = (view as TextInputLayout).error
            assertEquals(actualError, expectedError)
        }

}

我最喜欢这种方式,因为它可以灵活处理任何视图

答案 6 :(得分:-4)

请使用EditText的setError()方法 例如:EditText emailEditText;

if(invalidLogin)
    emailEditText.setError("Error Message");