【问题标题】:Using espresso view matcher with hamcrest matchers将 espresso 视图匹配器与 hamcrest 匹配器一起使用
【发布时间】:2016-02-09 18:03:06
【问题描述】:

谁能告诉我为什么这不起作用

onView(withId(R.id.edt_apikey)).check(matches(hasErrorText(anyString())));

在 logcat 中显示:

android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with error: is ""' doesn't match the selected view.
Expected: with error: is ""
Got: "AppCompatEditText{id=2131492985, res-name=edt_apikey, visibility=VISIBLE, width=517, height=83, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=true, editor-info=[inputType=0x80001 imeOptions=0xc000005 privateImeOptions=null actionLabel=null actionId=0 initialSelStart=0 initialSelEnd=0 initialCapsMode=0x0 hintText=1c915e3b5d42d05136185030892fbb846c278927 label=null packageName=null fieldId=0 fieldName=null extras=null ], x=139.0, y=83.0, text=, error-text=This field is required, hint=1c915e3b5d42d05136185030892fbb846c278927, input-type=524289, ime-target=false, has-links=false}"

我猜上面的方法不起作用,因为它是一个 Mockito 匹配器。 所以我尝试了这个:

    import static android.support.test.espresso.matcher.ViewMatchers.hasErrorText;
import static org.hamcrest.Matchers.isEmptyOrNullString;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;

onView(withId(R.id.edt_apikey)).check(matches(hasErrorText(not(isEmptyOrNullString()))));

而且它也不起作用。给出这个例外......

java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.String java.lang.CharSequence.toString()' on a null object reference
                                                                     at android.support.test.espresso.matcher.ViewMatchers$34.matchesSafely(ViewMatchers.java:1130)
                                                                     at android.support.test.espresso.matcher.ViewMatchers$34.matchesSafely(ViewMatchers.java:1120)

【问题讨论】:

  • 在检查字段是否没有显示错误文本时遇到同样的空问题。您找到编写此测试用例的解决方案了吗?
  • 是报告在 android 支持工具问题列表中。我希望他们现在已经修好了。但无论如何,您可以创建一个自定义匹配器,在错误文本测试之前简单地测试 null
  • @harishannam 请查看我的回答并点赞

标签: java android testing android-espresso hamcrest


【解决方案1】:

临时解决办法是创建自定义匹配器

public static Matcher<View> hasErrorText(final Matcher<String> stringMatcher) {
    checkNotNull(stringMatcher);
    return new BoundedMatcher<View, EditText>(EditText.class) {

        @Override
        public void describeTo(Description description) {
            description.appendText("with error: ");
            stringMatcher.describeTo(description);
        }

        @Override
        protected boolean matchesSafely(EditText view) {
            if (view.getError() == null) return stringMatcher.matches(view.getError());
            return stringMatcher.matches(view.getError().toString());
        }
    };
}

然后像这样使用它 onView(withId(R.id.edt_apikey)).check(matches(hasErrorText(not(Matchers.isEmptyOrNullString()))));

【讨论】:

    【解决方案2】:

    看来你的测试代码:

        onView(withId(R.id.edt_apikey)).check(matches(hasErrorText(anyString())));
    

    返回null 对象。我没用过anything()Hamcrest 匹配器,但是如果你真的想检查错误信息是否为空,也许最好的选择是这样的代码:

         onView(withId(R.id.edt_apikey)).check(matches(withText("")));
    

    希望对你有帮助

    【讨论】:

    • 这将检查该字段的“文本”值,而不是来自 setError 属性的“错误”文本
    猜你喜欢
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-15
    • 2018-10-07
    • 1970-01-01
    相关资源
    最近更新 更多