首页 > 解决方案 > 在 Android 中测试验证

问题描述

嗨,让这个工作有问题,想点击登录按钮并测试验证错误是否出现。我正在使用材料设计验证,这就是我写的

   @Test
    public void click_login_with_empty_fields_gives_errors() {

        onView(withId(R.id.login)).perform(click());
        onView(withId(R.id.login_email2)).check(matches(hasErrorText("Email Field cannot be empty")));
        onView(withId(R.id.login_password2)).check(matches(hasErrorText("Password field cannot be empty")));
    }

我得到的错误是

androidx.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with error: is "Password field cannot be empty"' doesn't match the selected view.
Expected: with error: is "Password field cannot be empty"
Got: "TextInputEditText{id=2131362003, res-name=login_password2, visibility=VISIBLE, width=996, height=141, has-focus=false, has-focusable=true, has-window-focus=true

标签: androidvalidationmaterial-designandroid-espresso

解决方案


似乎您正在匹配 a TextInputEditTextin onView(Matcher)call 而不是 a TextInputLayout。错误文本设置在TextInputLayout而不是它的 child EditText。看这个测试

编辑:您还应该建议使用自定义匹配器而不是hasErrorText().


推荐阅读