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

7

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

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

我的问题是:如何选择属于层次结构的各个TextView。

例如:如何获取当前月份 > 销售额 > 值 视图

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

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个回答

13

好的。看起来相当简单。

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

那是最好的方法吗?


也许你可以帮我解决这个问题 https://stackoverflow.com/questions/67199041/espresso-tap-on-specific-bottomnavigationbar-item - StuartDTO

1
使用`withParent(Matcher)`层次视图匹配器:
onView(allOf(withId(R.id.value),
     withParent(allOf(withId(R.id.sales),
          withParent(withId(R.id.current_month)))),
  isDisplayed()));

希望这有所帮助。

也许你可以在这里提供指导 https://stackoverflow.com/questions/67199041/espresso-tap-on-specific-bottomnavigationbar-item - StuartDTO

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