使用 Android Espresso 在层次结构中查找视图

Find view within hierarchy with Android Espresso

我有一个带有自定义视图的仪表板。

我想测试一下,一旦数据可用,它就会在各种文本视图中正确显示。

我的问题是:如何 select 属于一个层次结构的各种 TextView。

例子:如何得到current_month > sales > value view?

我只知道如何处理单个层次结构级别,但这对这里没有帮助:

onView(
  allOf(withId(R.id.value), 
  isDescendantOfA(withId(R.id.current_month))))

层次结构如下:

+- RelativeLayout
|
|___+ CustomCardView (id = current_month)
|   |
|   |___+ CustomValueView (id = sales)
|   |   |
|   |   |___+ TextView (id = value)
|   |   |
|   |   |___+ TextView (id = unit)
|   |
|   |___+ CustomValueView (id = earnings)
|       |
|       |___+ TextView (id = value)
|       |
|       |___+ TextView (id = unit)
|
|___+ CustomCardView (id = last_month)
|   |
|   |___+ CustomValueView (id = sales)
|   |   |
|   |   |___+ TextView (id = value)
|   |   |
|   |   |___+ TextView (id = unit)
|   |
|   |___+ CustomValueView (id = earnings)
|       |
|       |___+ TextView (id = value)
|       |
|       |___+ TextView (id = unit)
|
|___+ CustomCardView (id = all_time)
    |
    |___+ CustomValueView (id = sales)
    |   |
    |   |___+ TextView (id = value)
    |   |
    |   |___+ TextView (id = unit)
    |
    |___+ CustomValueView (id = earnings)
        |
        |___+ TextView (id = value)
        |
        |___+ TextView (id = unit)

好的。看起来比较简单

    onView(allOf(withId(R.id.value),
            isDescendantOfA(allOf(withId(R.id.sales),
                    isDescendantOfA(withId(R.id.current_month))))))

这是最好的方法吗?

使用withParent(Matcher)层次结构视图匹配器:

onView(allOf(withId(R.id.value),
     withParent(allOf(withId(R.id.sales),
          withParent(withId(R.id.current_month)))),
  isDisplayed()));

希望对您有所帮助。