【问题标题】:Using Espresso to Unit Test Google Maps使用 Espresso 对 Google 地图进行单元测试
【发布时间】:2015-04-28 16:11:45
【问题描述】:

我正在使用 Espresso 对我的应用进行一些 UI 测试。我有一个带有地图的片段,并在其上显示了通过调用后端获得的一些项目。

当我点击一个标记时,我正在做一些 UI 操作

有什么方法可以在我的地图上使用 espresso 进行单元测试?

【问题讨论】:

  • 对于Google maps Espresso test,请查看here
  • 能否给我一个点击标记的简单示例?
  • 好的,去我的github看看herehere
  • @bjiang ,不清楚如何处理您的样品。您可以更新自述文件以使其更容易吗? IE。选择标记的代码在哪里?
  • @bjiang 链接(及其重定向)已损坏。

标签: android google-maps android-espresso


【解决方案1】:

简答: 用浓缩咖啡是不可能的。 一个解决方案可能是使用 UIAutomator: https://developer.android.com/tools/testing-support-library/index.html#UIAutomator https://developer.android.com/training/testing/ui-testing/uiautomator-testing.html

所以你需要:

1) 添加gradle依赖:

dependencies {
androidTestCompile 'com.android.support.test:runner:0.2'
androidTestCompile 'com.android.support.test:rules:0.2'
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1' }

2) 确保至少在标记中添加标题,即使您不使用它。

3) 编写测试,代码是这样的:

UiDevice device = UiDevice.getInstance(getInstrumentation());
UiObject marker = device.findObject(new UiSelector().descriptionContains("marker title"));
marker.click();

说明:

GoogleMap 生成 UI 并使其易于访问,即地图内容可以被视为可访问性节点信息树。

This is tree 是一棵虚拟视图树,它并不代表真实的视图树。 我们稍后会谈到这个

默认情况下,地图的 contentDescription 为“Google Map”,标记的 contentDescription 为“{markerTitle}.{markerSnippet}”。

那么问题是为什么不使用浓缩咖啡:

onView(withContentDescription("marker title. ")).perform(click()); ?

因为它不会找到它,但是:

onView(withContentDescription("Google Map")).perform(click());

会正常工作的。

那么为什么 UIAutomator 可以工作而 Espresso 不能呢?

因为它们使用不同的视图树。

UIAutomator 使用 AccessibilityService 提供的可访问性节点信息树,而 Espresso 使用视图层次结构并因此处理任何 ViewGroup 的所有子级。可访问性节点信息和视图层次结构可能会或可能不会一对一映射。 在这种情况下

onView(withContentDescription("Google Map"))

找到的不是 ViewGroup 而是 TextureView,它不知道有孩子,所以 Espresso 不知道那里画了什么。

瞧! :)

【讨论】:

  • 谢谢,答案很清楚。我很惊讶像地图测试这样基本的东西在 Espresso 上是不可能的。
  • 这很好用!虽然当我使用 ./gradlew connectedAndroidTest 运行所有测试时它不起作用,但它必须作为单个测试运行
  • 我相信问题不在于这种方法,而在于您运行所有测试的方式。如果您向我提供更详细的问题,我很乐意提供帮助:)
  • 我有同样的问题 @JesusAlmaral 我只是从 android studio 运行所有案例,相当于 ./gradlew connectedAndroidTest。
  • 再次,这取决于其他测试做什么以及应用程序的行为。如果是这种情况,请尝试放置等待/超时或使用 clickAndWaitForNewWindow
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多