android - 使用 Android Espresso 在层次结构中查找 View

标签 android android-espresso

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

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

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

这是最好的方法吗?

关于android - 使用 Android Espresso 在层次结构中查找 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42094681/

相关文章:

android - 多种设备密度和 Xamarin Android

带有 Dagger 的 Android Espresso

android - AsyncTask 运行时的 Espresso 测试后退按钮

android - 更改 androidTest list 中的 manifestPlaceholders

android - 如何使用 deviceCheck 任务在远程设备上运行测试

java - 从 PHP MySql 填充 Android spinner

android - 如何在 android gradle 构建文件中为从 maven 中心提取的库指定 injars

java - 如何将 Espresso 2(用于测试)与扩展应用程序(android.app.Application)一起使用?

安卓 Google Fit onConnectionFailed() SIGN_IN_REQUIRED

java - Google Maps Android v2 - 无法隐藏 map fragment