繁体   English   中英

使用Espresso对Google Maps进行单元测试

[英]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的所有子级。 可访问性节点信息和视图层次结构可能一对一映射,也可能不一对一映射。 在这种情况下

onView(withContentDescription(“ Google Map”))

查找的不是ViewGroup而是TextureView,后者不知道有子级,因此Espresso无法知道其中绘制了什么。

瞧! :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM