如何单击ListView特定行位置中的视图

Ger*_*ári 3 position row ui-testing android-listview android-espresso

我有一个ListView:

在此输入图像描述

我想点击ListView中的特定按钮.

如果我想用onData选择器选择:

onData(withId(R.id.button))
                .inAdapterView(withId(R.id.list_view))
                .atPosition(1)
                .perform(click());
Run Code Online (Sandbox Code Playgroud)

我收到这个错误:

android.support.test.espresso.PerformException: Error performing 'load adapter data' on view 'with id: com.example.application:id/list_view'.
...
Run Code Online (Sandbox Code Playgroud)

我怎么解决这个问题?

Be_*_*ive 7

onData()需要对您感兴趣的项目使用对象匹配器.如果您不关心适配器中的数据,可以使用它Matchers.anything()来有效地匹配适配器中的所有对象.或者,您可以为项目创建数据匹配器(取决于存储在适配器中的数据)并将其传递给更确定的测试.

至于按钮 - 你正在寻找的是一种onChildsView()方法,它允许传递listitem的后代的一个viewmatcher,它在onData().atPosition()

结果你的测试看起来像这样:

    onData(anything()).inAdapterView(withId(R.id.list_view))
            .atPosition(1)
            .onChildView(withId(R.id.button))
            .perform(click());
Run Code Online (Sandbox Code Playgroud)