【问题标题】:Espresso - check if the TextView exists in the ListViewEspresso - 检查 ListView 中是否存在 TextView
【发布时间】:2016-01-06 10:16:43
【问题描述】:

我想查看Save €XX在列表中的显示。 Save €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.

有没有办法用 Espresso 判断 TextView 是否存在于 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: ...]>

【问题讨论】:

    标签: android junit junit4 android-espresso


    【解决方案1】:

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

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

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

    像魅力一样工作。享受吧!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-08
      • 2018-02-15
      • 2019-10-03
      • 2011-08-16
      • 1970-01-01
      • 1970-01-01
      • 2013-03-12
      • 1970-01-01
      相关资源
      最近更新 更多