从Espresso的下拉列表中选择一个项目

时间:2019-05-23 20:57:16

标签: android-espresso android-adapterview

我正在尝试编写Espresso测试,该测试将导航到搜索,键入2个字母以获得增量搜索,然后点击列表中的第二项。

结果显示在适配器中。

当然,我在第一部分做得很好,但是当我需要单击列表中的项目时,就会出现问题。

我已经尝试过

onData(anything()).inAdapterView(withId(R.id.searchContainer)).atPosition(1).perform(click())

onData(withChild(withText("Some Search"))).atPosition(1).perform(click())

(其中“某些搜索”是结果的全文)

onData(withId(android.R.id.text1)).atPosition(1).perform(click())

(其中text1是结果字段的ID)

    @Test
    fun searchForSandwich() {

        onView(withId(R.id.textSearch)).perform(typeText("ha"))

        onData(withChild(withText("Ham Sandwich"))).atPosition(1).perform(click())

它应该单击结果并打开新的屏幕/活动,我将在此处进行声明。

当前,我收到此错误:

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 assignable from class: class android.widget.AdapterView and is displayed on the screen to the user)
Target view: "ce{id=-1, visibility=GONE, width=0, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=true, is-selected=false, layout-params=android.widget.RelativeLayout$LayoutParams@4d102dd, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=0}"

任何建议,它使我发疯...谢谢:)

1 个答案:

答案 0 :(得分:0)

我需要查看视图以确保百分之99的人不需要使用onData,并且可以摆脱onView(withText(containsString(yourSearchFilter)))。perform(click())。

可能还有一个withSubstring(text)对应于withText(containsString(text))