Android 中的测试验证
Testing validation in Android
您好,在使它正常工作时遇到问题,想单击登录按钮并测试是否出现验证错误。我正在使用 material 设计验证,这是我写的
@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
您似乎在 onView(Matcher)
调用中匹配 TextInputEditText
而不是 TextInputLayout
。错误文本设置在 TextInputLayout
而不是其子 EditText
中。参见 this test。
编辑:您还应该检查 建议使用自定义匹配器而不是 hasErrorText()
。
您好,在使它正常工作时遇到问题,想单击登录按钮并测试是否出现验证错误。我正在使用 material 设计验证,这是我写的
@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
您似乎在 onView(Matcher)
调用中匹配 TextInputEditText
而不是 TextInputLayout
。错误文本设置在 TextInputLayout
而不是其子 EditText
中。参见 this test。
编辑:您还应该检查 hasErrorText()
。