使用 UiAutomator 测试地图是否已加载

Using UiAutomator to test if map has loaded or not

我是 android 中的新手。我有一张地图 activity,它在 MainActivity 中的一些事件之后被调用,并且地图出现在屏幕上。我不知道如何测试地图是否出现。 我知道使用浓缩咖啡是不可能的,但是在某处读到它可以使用 UiAutomator。如何使用 UiAutomator 实现同样的..?

您可以设置托管地图的视图的内容描述来指示其"readiness"。

    ...
    SupportMapFragment mapFragment =
            (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
    mapView = mapFragment.getView();
    mapView.setContentDescription("MAP NOT READY");
    // you may need to preserve existing description
    mapFragment.getMapAsync(this);



@Override
public void onMapReady(GoogleMap googleMap) {
    ...
    mapView.setContentDescription("MAP READY");
 }  

然后,在你的测试中

mDevice.wait(Until.hasObject(By.desc("MAP READY")), timeout);

这个解决方案对我有用: 我刚刚更新了@Diego 的解决方案,因为我在这一行得到了 ClassCastException mapView = mapFragment.getView(); 所以我删除了此行并没有在初始设置内容描述,因为默认情况下 googleMap 的内容描述为 "Google Map",因此无需设置它,只需在内部更新它即可:

@Override
public void onMapReady(GoogleMap googleMap) {
    ...
    mapView.setContentDescription("Google Map Ready");
 }   

然后等待你的测试class喜欢:

mDevice.wait(Until.hasObject(By.desc("Google Map Ready")), timeout);