问题描述
我正在使用 Espresso 对我的应用进行一些 UI 测试.我有一个带有地图的片段,我在上面显示了一些通过调用后端获得的项目.
I am using Espresso to do some UI testing on my app. I have a fragment with a map, and I display some items on it that I get through a call to my backend.
当我点击一个标记时,我正在做一些 UI 的事情
When I click on a marker, I'm doing some UI things
有什么办法可以用 espresso 在我的地图上进行单元测试吗?
Is there any way I can do unit testing on my map with espresso ?
推荐答案
简短回答:用意式浓缩咖啡是不可能的.一个解决方案可能是使用 UIAutomator:https://developer.android.com/tools/testing-support-library/index.html#UIAutomatorhttps://developer.android.com/training/testing/ui-testing/uiautomator-testing.html
Short Answer:With espresso is not really possible.A solution might be to use UIAutomator:https://developer.android.com/tools/testing-support-library/index.html#UIAutomatorhttps://developer.android.com/training/testing/ui-testing/uiautomator-testing.html
所以你需要:
1) 添加 gradle 依赖:
1) add gradle dependencies:
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) 即使您不使用它,也要确保至少为标记添加标题.
2) make sure you add at least title to your markers even if you're not using it.
3) 编写测试,代码是这样的:
3) Write the test, code is smth like this:
UiDevice device = UiDevice.getInstance(getInstrumentation());
UiObject marker = device.findObject(new UiSelector().descriptionContains("marker title"));
marker.click();
说明:
GoogleMap 生成 UI 并使其可访问,即地图内容可以被视为可访问性节点信息的树.
GoogleMap generates UI and also makes it accessible i.e. map content can be seen as a tree of accessibility node info.
这是一个虚拟的视图树,不代表真实的视图树.我们稍后会谈到这个
This is tree is a virtual view tree, it does not represent the real view tree.We will come to this later
默认地图的内容描述为谷歌地图",标记的内容描述为{markerTitle}.{markerSnippet}".
By default the contentDescription of the map is "Google Map" and that of markers is "{markerTitle}. {markerSnippet}".
那么问题是为什么不使用浓缩咖啡:
Then the question is so why not using espresso:
onView(withContentDescription("marker title.")).perform(click());
?
因为它不会找到它,但是:
Because it won't find it, however :
onView(withContentDescription("Google Map")).perform(click());
会工作得很好.
为什么 UIAutomator 可以工作而 Espresso 不行?
因为它们使用不同的视图树.
UIAutomator 使用 AccessibilityService 提供的可访问性节点信息树,而 Espresso 使用视图层次结构,从而处理任何 ViewGroup 的所有子级.可访问性节点信息和视图层次结构可能一一映射,也可能不一一映射.在这种情况下
UIAutomator uses tree of accessibility node info provided by AccessibilityService, while Espresso uses the view hierarchy and thus processes all children of any ViewGroup. The accessibility node info and the view hierarchy may or may not map one-to-one.In this case
onView(withContentDescription("Google Map"))
找到的不是 ViewGroup 而是 TextureView,它不知道有孩子,所以 Espresso 无法知道那里画了什么.
finds not a ViewGroup but a TextureView, which is not known to have children so Espresso cannot know what is drawn in there.
瞧!:)
这篇关于使用 Espresso 对 Google Maps 进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!