Android - 测试Google地图信息窗口点击

时间:2017-02-28 09:47:40

标签: android google-maps testing android-espresso uiautomator

我正在为我的应用编写测试,但我仍然坚持测试GoogleMap。使用UiAutomator轻松点击Marker,但似乎无法点击信息窗口。

这就是我点击Marker点击

的方式
UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
UiObject marker = device.findObject(new UiSelector().descriptionContains(MARKER_TITLE));

我尝试使用Android设备监视器并转储视图层次结构,但似乎该视图(信息窗口)不存在。

以下是我尝试测试的屏幕的样子: https://i.stack.imgur.com/FuxHG.png

有关如何使用EspressoUiAutomator点击信息窗口的想法吗?

3 个答案:

答案 0 :(得分:1)

The info window that is drawn is not a live view. The view is rendered as an image (using View.draw(Canvas)) at the time it is returned. This means that you won't be able to find it as an Object.

As you can listen to a generic click event on the whole info window, you might be able to touch is using its coordinates calculated as an offset from the marker.

答案 1 :(得分:0)

某些网页视图无法使用uiautomator自动完成。无论uiautomatorviewer中出现的ui元素是什么,都可以自动化。只需先检查uiautomatorviewer即可。如果它没有出现在查看器中,则可以使用adb shell input tap <x> <y>。其中x,y是屏幕上的坐标。

答案 2 :(得分:0)

正如迭戈·托雷斯·米兰诺(Diego Torres Milano)所述,您不能使用Espresso,因为InfoWindow是作为图像绘制的。但是您可以使用UIAutomator,并假定单击该标记后会将其动画化到屏幕中央:

app / build.gradle

androidTestImplementation 'com.android.support.test.uiautomator:uiautomator-v18:2.1.3'

MapViewTest.kt

@RunWith(AndroidJUnit4::class)
class MapViewTest {
  @Rule
  @JvmField
  val activityRule: ActivityTestRule<MapViewActivity> 
    = ActivityTestRule(MapViewActivity::class.java)

  @Test
  fun mapViewUITest() {
    val twoSeconds = 2000
    val fiveSeconds = 5000

    // Wait for the Map View to load
    waitFor(fiveSeconds)

    // First of all, click on the Marker, using UIAutomator
    val device = UiDevice.getInstance(getInstrumentation())
    val marker = device.findObject(UiSelector().descriptionContains("My Marker Title"))
    marker.click()

    // After a marker is clicked, the MapView will automatically move the map
    // to position the Marker at the center of the screen, at 43% screen height.
    // So wait for the animation to finish first.
    waitFor(twoSeconds)

    // Calculate the (x,y) position of the InfoWindow, using the screen size
    val display = activityRule.activity.windowManager.defaultDisplay
    val size = Point()
    display.getRealSize(size)
    val screenWidth = size.x
    val screenHeight = size.y
    val x = screenWidth / 2
    val y = (screenHeight * 0.43).toInt()

    // Click on the InfoWindow, using UIAutomator
    device.click(x, y)
    waitFor(twoSeconds)
  }

  private fun waitFor(duration: Int) = Thread.sleep(duration.toLong())
}