Espresso - 检查TextView是否存在于ListView中

8

我希望检查列表中的Save €XX显示情况。 Save €XX是一个可以为VISIBLEINVISIBLE的TextView。 我使用JUnit 4和Espresso 2.2.1。

我尝试像这样检查:

onView(withText(startsWith("Save"))).check(matches(isDisplayed()));

但是总是出现错误:

android.support.test.espresso.AmbiguousViewMatcherException: 'with text: a string starting with "Save"' matches multiple views in the hierarchy.

有没有一种方法可以使用Espresso判断ListView中是否存在TextView?
更新:
我还尝试使用onData:
onData(hasToString(startsWith("Save")))
                .inAdapterView(withId(R.id.suggestion_list_view)).atPosition(0)
                .check(matches(isDisplayed()));

但是看起来 onData 只能用于数据层而无法用于视图层。因此,我收到以下错误信息:

java.lang.RuntimeException: No data found matching: with toString() a string starting with "Save" contained values: <[Data: ...]>

enter image description here

1个回答

18

尝试了几次后,我找到了方法。

在这种情况下,我们应该采用综合方法并同时处理数据和视图层。我们通过ID访问 ListView 并选择第一个项目。然后检查是否有'Save'文本。

onData(anything())
                .inAdapterView(withId(R.id.list_view))
                .atPosition(0)
                .onChildView(withId(R.id.suggestion_saving))
                .check(matches(withText(startsWith("Save"))));

运行得很好,享受吧!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接