android expresso测试失败调用onview两次

时间:2018-03-20 17:25:11

标签: android unit-testing testing android-espresso

我是为android编写的testinig代码,我正在使用JUnit4和from django.conf.urls import url from . import views from django.contrib.auth import views as auth_views app_name = 'visit' urlpatterns = [ url(r'^$', views.index, name='index'), url(r'login/$', auth_views.login, {'template_name': 'visit/registration/login.html'}, name='login'), # url(r'accounts/profile/', views.profile, name='profile'), # need to change the app directory from here url(r'^register/', views.registration, name='registration'), ] 。 如果我在之前调用的android对象上调用两次android.support.test.espresso.Espresso.*,如onviewEditTextTexView,或者我得到异常,我不知道是否会发生这种情况我错过了什么或者它是一个错误,如何解决这个问题?

Button

错误日志:

onView(withId(R.id.name))
        .perform(typeText(NAME), closeSoftKeyboard());
onView(withId(R.id.surname))
        .perform(typeText(SURNAME), closeSoftKeyboard());
onView(withId(R.id.name))
        .perform(replaceText(NAME), closeSoftKeyboard());

布局xml:

android.support.test.espresso.PerformException: Error performing 'replace text' on view 'with id: com.myapp:id/name'.
    at android.support.test.espresso.PerformException$Builder.build(PerformExceptio 
 n.java:84)

    at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:81)
    at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:52)
    at android.support.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:312)
    at android.support.test.espresso.ViewInteraction.desugaredPerform(ViewInteraction.java:167)
    at android.support.test.espresso.ViewInteraction.perform(ViewInteraction.java:110)
    at com...
Caused by: java.lang.RuntimeException: Action will not be performed because the target view does not match one or more of the following constraints:
(is displayed on the screen to the user and is assignable from class: class android.widget.EditText)

1 个答案:

答案 0 :(得分:1)

根据日志,name无法分配视图EditText。请参阅ViewActions.replaceText()

的文档

您需要为此编写自己的ViewAction。 来自其他帖子thisthis 像下面一样创建自己的ViewAction

public static ViewAction setTextInTextView(final String value){
    return new ViewAction() {
        @SuppressWarnings("unchecked")
        @Override
        public Matcher<View> getConstraints() {
            return allOf(isDisplayed(), isAssignableFrom(TextView.class));
            //                                            ^^^^^^^^^^^^^^^^^^^
            // To check that the found view is TextView or it's subclass like EditText
            // so it will work for TextView and it's descendants
        }

        @Override
        public void perform(UiController uiController, View view) {
            ((TextView) view).setText(value);
        }

        @Override
        public String getDescription() {
            return "replace text";
        }
    };
}

然后像这样替换你的代码

 onView(withId(R.id.name))
            .perform(setTextInTextView(NAME), closeSoftKeyboard());
 onView(withId(R.id.surname))
            .perform(setTextInTextView(SURNAME), closeSoftKeyboard());
 onView(withId(R.id.name))
            .perform(setTextInTextView(NAME), closeSoftKeyboard());