首页 > 解决方案 > Espresso 不会在 SearchView 上执行 typeText ViewAction

问题描述

我的代码如下所示:

onView(withId(R.id.search_view)).perform(click());
onView(withId(R.id.search_view)).perform(typeText(search_query));

出于某种原因,即使我看到搜索栏和键盘短暂显示并且目标视图是 SearchView,但我收到此错误:

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 (supports input methods or is assignable from class: class android.widget.SearchView))
Target view: "SearchView{id=2131362837, res-name=search_view, visibility=VISIBLE, width=1080, height=126, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.FrameLayout$LayoutParams@940faa7, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}"

标签: android

解决方案


您收到运行时错误,因为您尝试执行typeText不可SearchView编辑的字段。SearchView只是一个LinearLayout包含可编辑字段的简单字段,因此您应该将匹配器更改为:

onView(allOf(supportsInputMethods(), isDescendantOfA(withId(R.id.search_view))))
    .perform(typeText(search_query));

推荐阅读