使用 Espresso 对地图进行单元测试 Google
Using Espresso to Unit Test Google Maps
我正在使用 Espresso 对我的应用进行一些 UI 测试。我有一个带有地图的片段,我在上面显示了一些通过调用后端获得的项目。
当我点击标记时,我正在做一些 UI 事情
有什么方法可以用 espresso 在我的地图上进行单元测试吗?
简答:
用意式浓缩咖啡是不可能的。
一个解决方案可能是使用 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 并使其可访问,即地图内容可以被视为可访问节点信息树。
这是一棵虚拟的视图树,不代表真实的视图树。
我们稍后会谈到这个
默认情况下,地图的 contentDescription 是 "Google Map",标记的 contentDescription 是“{markerTitle}. {markerSnippet}”。
那么问题是为什么不使用浓缩咖啡:
onView(withContentDescription("marker title. ")).perform(click());
?
因为它找不到,但是:
onView(withContentDescription("Google Map")).perform(click());
会很好用。
那么为什么 UIAutomator 工作而 Espresso 不工作?
因为他们使用了不同的视图树。
UIAutomator 使用 AccessibilityService 提供的无障碍节点信息树,而 Espresso 使用视图层次结构,从而处理任何 ViewGroup 的所有 children。可访问性节点信息和视图层次结构可能映射也可能不映射 one-to-one。
在这种情况下
onView(withContentDescription("Google Map"))
找到的不是 ViewGroup,而是 TextureView,不知道它有 children 所以 Espresso 无法知道那里画了什么。
瞧! :)
我正在使用 Espresso 对我的应用进行一些 UI 测试。我有一个带有地图的片段,我在上面显示了一些通过调用后端获得的项目。
当我点击标记时,我正在做一些 UI 事情
有什么方法可以用 espresso 在我的地图上进行单元测试吗?
简答: 用意式浓缩咖啡是不可能的。 一个解决方案可能是使用 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 并使其可访问,即地图内容可以被视为可访问节点信息树。
这是一棵虚拟的视图树,不代表真实的视图树。 我们稍后会谈到这个
默认情况下,地图的 contentDescription 是 "Google Map",标记的 contentDescription 是“{markerTitle}. {markerSnippet}”。
那么问题是为什么不使用浓缩咖啡:
onView(withContentDescription("marker title. ")).perform(click());
?
因为它找不到,但是:
onView(withContentDescription("Google Map")).perform(click());
会很好用。
那么为什么 UIAutomator 工作而 Espresso 不工作?
因为他们使用了不同的视图树。
UIAutomator 使用 AccessibilityService 提供的无障碍节点信息树,而 Espresso 使用视图层次结构,从而处理任何 ViewGroup 的所有 children。可访问性节点信息和视图层次结构可能映射也可能不映射 one-to-one。 在这种情况下
onView(withContentDescription("Google Map"))
找到的不是 ViewGroup,而是 TextureView,不知道它有 children 所以 Espresso 无法知道那里画了什么。
瞧! :)