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

13

基本上,我正在尝试测试在登录不正确后,邮箱字段中是否显示错误。

这个视图是:

<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个回答

24

这可以与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) {
        }
    };
}

4
不知道为什么Espresso的hasErrorText匹配器无法正常工作,但这个自定义匹配器完美地发挥了作用。 - sam9046
谢谢,兄弟!!你让我的一天变得美好了! - sVd
巨大的拇指向上 - martinseal1987

6

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

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

4
你可以编写自定义匹配器:

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

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

您可以随后使用它与。
matches(CustomItemMatchers.withErrorName(equalTo("My Error")))

这段代码是使用Espresso 1编写的,但我希望它仍然可以运行。


2
如果您不想使用自定义匹配器,那么在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)
        }

}

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


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

}


-1

它正在工作中 :)

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

不是,com.google.android.material.textfield.TextInputLayout。 - martinseal1987

-6
请使用EditText的setError()方法 例如:EditText emailEditText;
if(invalidLogin)
    emailEditText.setError("Error Message");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接