Android,如何测试Google地图
Android, how to test Google Map
所以,我在测试 Google 地图功能时遇到了问题。在我的活动中,我有回调 onMapClick(LatLng latlng)
,其中有 show/hide 工具栏的方法。现在,我想测试一下,但我不知道如何执行点击地图。我试着用这个:
onView(withContentDescription("Mapa Google")).perform(click());
还有这个:
UiDevice device = UiDevice.getInstance(getInstrumentation());
UiObject map = device.findObject(new UiSelector()
.descriptionContains("Mapa Google"));
map.click();
但它接缝,它不起作用。你知道我该如何测试这种行为吗?
Google Map Android version 2, 一般我们用fragment。从您的地图片段将地图同步侦听器添加到方法 "getMapAsync" 然后当 "onMapReady" 被调用时然后在那里设置地图点击回调
googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener(){
public void onMapClick(LatLng point){
// do something here
}
});
最好的方法是 UiAutomator 库,
https://developer.android.com/training/testing/ui-testing/uiautomator-testing.html
private UiDevice uiDevice = UiDevice.getInstance(getInstrumentation());
private UiObject mMarker1 = uiDevice.findObject(new UiSelector().descriptionContains("The title of marker"));
try {
mMarker1.click();
} catch (UiObjectNotFoundException e) {
e.printStackTrace();
}
要在地图上录音,您可以使用此
uiDevice.click(x, y);
x和y是坐标
根据我的知识和研究,您不能 直接模拟地图点击或标记点击。如果你真的想实现这一点,你可以尝试在 webview 中加载你的地图,并通过 MapsEvents API 模拟 javascript 中的点击。至少对于标记...(我知道它不一定是你在找什么,但你要测试的块可以从 OnMapClickListener 移动到 OnMarkerClickListener。如果它在那里工作,没有理由为什么它不能工作新回调)
如果我找到更好的工作解决方案,我会更新我的答案。
//在V2版本中:
event.trigger(mapMarker[i], 'click');
//在V3版本中:
google.maps.event.trigger(mapMarker[i], 'click');
所以,我在测试 Google 地图功能时遇到了问题。在我的活动中,我有回调 onMapClick(LatLng latlng)
,其中有 show/hide 工具栏的方法。现在,我想测试一下,但我不知道如何执行点击地图。我试着用这个:
onView(withContentDescription("Mapa Google")).perform(click());
还有这个:
UiDevice device = UiDevice.getInstance(getInstrumentation());
UiObject map = device.findObject(new UiSelector()
.descriptionContains("Mapa Google"));
map.click();
但它接缝,它不起作用。你知道我该如何测试这种行为吗?
Google Map Android version 2, 一般我们用fragment。从您的地图片段将地图同步侦听器添加到方法 "getMapAsync" 然后当 "onMapReady" 被调用时然后在那里设置地图点击回调
googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener(){
public void onMapClick(LatLng point){
// do something here
}
});
最好的方法是 UiAutomator 库, https://developer.android.com/training/testing/ui-testing/uiautomator-testing.html
private UiDevice uiDevice = UiDevice.getInstance(getInstrumentation());
private UiObject mMarker1 = uiDevice.findObject(new UiSelector().descriptionContains("The title of marker"));
try {
mMarker1.click();
} catch (UiObjectNotFoundException e) {
e.printStackTrace();
}
要在地图上录音,您可以使用此
uiDevice.click(x, y);
x和y是坐标
根据我的知识和研究,您不能 直接模拟地图点击或标记点击。如果你真的想实现这一点,你可以尝试在 webview 中加载你的地图,并通过 MapsEvents API 模拟 javascript 中的点击。至少对于标记...(我知道它不一定是你在找什么,但你要测试的块可以从 OnMapClickListener 移动到 OnMarkerClickListener。如果它在那里工作,没有理由为什么它不能工作新回调)
如果我找到更好的工作解决方案,我会更新我的答案。
//在V2版本中:
event.trigger(mapMarker[i], 'click');
//在V3版本中:
google.maps.event.trigger(mapMarker[i], 'click');