使用Android上的Espresso测试EditText视图是否未设置错误文本?
问题描述:
我知道如何测试EditText
中是否设置了错误文本:
I know how to test if an error text is set in an EditText
:
editText.check(matches(hasErrorText("")));
现在,我想测试EditText
是否未设置错误文本.我已经尝试过了,但是没有用.
Now I want to test if an EditText
has no error text set. I've tried this, but it does not work.
editText.check((matches(not(hasErrorText("")))));
有人知道怎么做吗?谢谢!
Does anyone know how to do that? Thanks!
答
我认为不可能那样做,具体取决于您想要的是什么,我会使用自定义匹配器:
I don't think it's possible that way, depending on what you want exactly, I would use a custom matcher:
public static Matcher<View> hasNoErrorText() {
return new BoundedMatcher<View, EditText>(EditText.class) {
@Override
public void describeTo(Description description) {
description.appendText("has no error text: ");
}
@Override
protected boolean matchesSafely(EditText view) {
return view.getError() == null;
}
};
}
此匹配器可以检查EditText是否未设置任何错误文本,请按以下方式使用它:
This matcher can check if an EditText does not have any error text set, use it like this:
onView(allOf(withId(R.id.edittext), isDisplayed())).check(matches(hasNoErrorText()));