java - Espresso onData 在 View 上执行 'load adapter data' 时出错

标签 java android testing android-espresso

我有一个app,有ListView,我想找LinearLayout,id=order_untake_jijia_listview_jia

enter image description here

代码是:

onData(withClassName(endsWith("ListView")))
            .inAdapterView(allOf(withId(R.id.order_untake_jijia_listview_ll), hasSibling(withText("9.0"))))
            .atPosition(0).onChildView(withId(R.id.order_untake_jijia_listview_jia));
            dataInteraction.perform(ViewActions.click());

但是我有错误:

Error performing 'load adapter data' on view '(with id: com.edaixi:id/order_untake_jijia_listview_ll and has sibling: with text: is "9.0")'.
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: "LinearLayout{id=2131493753, res-name=order_untake_jijia_listview_ll, visibility=VISIBLE, width=170, height=50, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=572.0, y=25.0, child-count=3}"

最佳答案

您错误地使用了 onDataonData 采用对象 匹配器 - 它旨在匹配用于填充列表的自定义模型对象。因此,如果您有一个简单字符串列表,您会说 onData(instanceOf(String.class)).atPosition(0)onData(is("This exact String at position 0"))

inAdapterView 用于匹配包含您的数据的特定适配器 View 实例。当屏幕上有多个列表需要区分时,就需要这个。例如,如果您有一个名为 R.id.list1ListView 和第二个 R.id.list2 都包含 Strings,你会说 onData(is("The String")).inAdapterView(withId(R.id.list1)) 来匹配第一个列表中的对象。

如果您只需要单击适配器中的单个项目,那么在这些情况下您通常也不需要指定 subview 。您通常使用 onChildView 对 subview 项进行断言,或者如果您碰巧在该 subview 上有一个单独的专用点击监听器。

因此,您似乎使情况过于复杂,您应该能够将代码更新为如下所示:

onData(instanceOf(MyCustomClassOfObjectsInAdapter.java))
    .atPosition(0)
    .perform(click());

如果这不能解决您的问题,请提供一些布局代码,说明您的列表项的外观以及具有 ListView 的主屏幕布局,以及带有显示示例的屏幕截图,并说明您的内容如果您尝试匹配/采取行动/断言,我可以尝试指导您找到完成所需任务所需的正确方法集。

希望对您有所帮助!

关于java - Espresso onData 在 View 上执行 'load adapter data' 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37184933/

相关文章:

Java 8 流列表收集器内存分配速度与带有预分配的循环

Android ListPreference 返回错误值

android - 如何在 Xamarin.Android 中使用 mipmap 文件夹?

javascript - 测试未运行服务模拟 promise (.then)

testing - PHPUnit:本地 VS 全局安装

java - 获取 HTTP 状态 500 - org.hibernate.internal.util.config.ConfigurationException : Specified cfg. xml 文件不存在

java - Java VM奇怪的性能问题

java - 与字符串的合并排序

django - 无法在 Django 测试类中加载对象

android - android中synchronized中的return语句呢?