Espresso - 检查 ListView 中是否存在 TextView

Espresso - check if the TextView exists in the ListView

我想查看 Save €XX 在列表中的显示。 节省 €XX 是一个 TextView,可以是 VISIBLEINVISIBLE。我使用 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.

如果 TextView 存在于带有 Espresso 的 ListView 中,是否有办法?

更新

我也试过用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: ...]>

经过几次尝试,我找到了路。

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

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

很有魅力。享受吧!