使用Espresso对Google地图进行单元测试

use*_*304 20 android google-maps android-espresso

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

当我点击标记时,我正在做一些UI事情

有什么方法可以在我的地图上用浓缩咖啡进行单元测试吗?

djo*_*djo 53

简答: 用浓咖啡是不可能的.解决方案可能是使用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' }
Run Code Online (Sandbox Code Playgroud)

2)确保即使您没有使用标记,也至少要为标记添加标题.

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

UiDevice device = UiDevice.getInstance(getInstrumentation());
UiObject marker = device.findObject(new UiSelector().descriptionContains("marker title"));
marker.click();
Run Code Online (Sandbox Code Playgroud)

说明:

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

这是树是虚拟视图树,它不代表真实视图树.我们稍后会谈到这个

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

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

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

但是因为它找不到它:

onView(withContentDescription("Google Map")).perform(click());
Run Code Online (Sandbox Code Playgroud)

会工作得很好.

那么为什么UIAutomator会起作用而Espresso却没有呢?

因为他们使用不同的视图树.

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

onView(withContentDescription("Google Map"))

发现不是ViewGroup而是一个不知道有孩子的TextureView,因此Espresso无法知道那里画的是什么.

瞧!:)

  • 谢谢,答案很清楚.我非常惊讶一些基本的东西,因为Espresso本身不可能进行地图测试. (9认同)