简答:
用浓缩咖啡是不可能的。
一个解决方案可能是使用 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 不知道那里画了什么。
瞧! :)