简体   繁体   English

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

[英]Using Espresso to Unit Test Google Maps

I am using Espresso to do some UI testing on my app. 我正在使用Espresso在我的应用程序上进行一些UI测试。 I have a fragment with a map, and I display some items on it that I get through a call to my backend. 我有一个带有地图的片段,并且在其中显示了一些我通过后端调用获得的项目。

When I click on a marker, I'm doing some UI things 当我点击一个标记时,我正在做一些UI事情

Is there any way I can do unit testing on my map with espresso ? 我可以用espresso在地图上进行单元测试吗?

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#UIAutomator https://developer.android.com/training/testing/ui-testing/uiautomator-testing.html 一种解决方案可能是使用UIAutomator: https : //developer.android.com/tools/testing-support-library/index.html#UIAutomator https://developer.android.com/training/testing/ui-testing/uiautomator -testing.html

So you need to: 因此,您需要:

1) add gradle dependencies: 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) make sure you add at least title to your markers even if you're not using it. 2)即使您没有使用标题,也请确保至少在标题中添加标题。

3) Write the test, code is smth like this: 3)编写测试,代码如下:

UiDevice device = UiDevice.getInstance(getInstrumentation());
UiObject marker = device.findObject(new UiSelector().descriptionContains("marker title"));
marker.click();

Explanation: 说明:

GoogleMap generates UI and also makes it accessible ie map content can be seen as a tree of accessibility node info. GoogleMap生成UI并使其可访问,即,地图内容可以看作是可访问性节点信息的树。

This is tree is a virtual view tree, it does not represent the real view tree. 这是树,是虚拟视图树,它不代表真实视图树。 We will come to this later 我们稍后再来

By default the contentDescription of the map is "Google Map" and that of markers is "{markerTitle}. {markerSnippet}". 默认情况下,地图的contentDescription为“ Google Map”,而标记的contentDescription为“ {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());

will work just fine. 会很好。

So how come UIAutomator works and Espresso doesn't? 那么UIAutomator如何工作而Espresso没有呢?

Because they use different view trees. 因为它们使用不同的视图树。

UIAutomator uses tree of accessibility node info provided by AccessibilityService, while Espresso uses the view hierarchy and thus processes all children of any ViewGroup. UIAutomator使用AccessibilityService提供的可访问性节点信息树,而Espresso使用视图层次结构,从而处理任何ViewGroup的所有子级。 The accessibility node info and the view hierarchy may or may not map one-to-one. 可访问性节点信息和视图层次结构可能一对一映射,也可能不一对一映射。 In this case 在这种情况下

onView(withContentDescription("Google Map")) onView(withContentDescription(“ Google Map”))

finds not a ViewGroup but a TextureView, which is not known to have children so Espresso cannot know what is drawn in there. 查找的不是ViewGroup而是TextureView,后者不知道有子级,因此Espresso无法知道其中绘制了什么。

Voila! 瞧! :) :)

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

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