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

时间:2017-02-07 16:12:09

标签: android android-espresso

我有一个包含自定义视图的信息中心。

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

我的问题是:如果它们属于层次结构,我如何选择各种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)

2 个答案:

答案 0 :(得分:8)

确定。看起来很简单。

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

这是最好的方法吗?

答案 1 :(得分:0)

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

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

希望这有帮助。