简体   繁体   English

如何在谷歌地图上点击 Xamarin UITest 的标记

[英]How to click on a marker on the google map for Xamarin UITest

I write Xamarin UITest for Android app.我为 Android 应用程序编写了 Xamarin UITest。 In the app uses google map.在应用程序中使用谷歌地图。 Help me please, how to click on a marker on the map?请帮帮我,如何点击地图上的标记?

markers are not showing within the tree of views, my guess is that they're drawn on screen within the map framelayout.标记未显示在视图树中,我的猜测是它们是在地图框架布局内的屏幕上绘制的。

given that the marker is at the center of the map, you could tap it using something like this :鉴于标记位于地图的中心,您可以使用以下内容点击它:

(with a map fragment of id "map") (带有 ID 为“map”的地图片段)

var map = app.Query("map")
var middleX = (map.First().Rect.Width + map.First().Rect.X) / 2
var middleY = (map.First().Rect.Height + map.First().Rect.Y) / 2
app.TapCoordinates(middleX, middleY)

but I think that's all you can do within the map itself.但我认为这就是你在地图中所能做的。

If you're building the app yourself, I suggest checking out backdoor methods.如果您自己构建应用程序,我建议您查看后门方法。 These are methods that you can build into the app and then call from the test.这些方法可以构建到应用程序中,然后从测试中调用。

There are plenty of examples for Obj-C and Java apps. Obj-C 和 Java 应用程序有很多示例。 Here's an example for C# backdoor methods, which you'd put in either your MainActivity or AppDelegate classes: http://danatxamarin.com/2015/05/07/configuring-backdoor-methods-in-c-for-xamarin-test-cloud/这是 C# 后门方法的示例,您可以将其放入 MainActivity 或 AppDelegate 类: http : //danatxamarin.com/2015/05/07/configuring-backdoor-methods-in-c-for-xamarin-test -云/

I'd create a backdoor to return whatever data you need about the map, and then use app.Invoke("myNewMethod");我会创建一个后门来返回你需要的关于地图的任何数据,然后使用app.Invoke("myNewMethod"); which will return a string (which could be json).这将返回一个字符串(可能是 json)。 This string could contain screen coordinates, which you could then pass to app.TapCoordinates(x, y);此字符串可以包含屏幕坐标,然后您可以将其传递给app.TapCoordinates(x, y); . .

The short answer is, Google Maps on Android doesn't really expose objects in an automatable way, so the second best option is backdoors.简短的回答是,Android 上的 Google 地图并没有真正以可自动化的方式公开对象,因此第二个最佳选择是后门。

If you have a custom renderer for android you can simulate a click inside, the main trick is to access markers inside the renderer:如果你有一个自定义的 android 渲染器,你可以在里面模拟点击,主要的技巧是访问渲染器内的标记:

var field = this.GetType().BaseType.GetField("_markers", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
var markers = (List<Marker>)field.GetValue(this);

now for any element in markers as you cannot send a real click you can just simulate it.现在对于markers任何元素,因为您无法发送真正的点击,您可以模拟它。 Pasting the code from my live project:粘贴来自我的实时项目的代码:

  protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);

        if (e.PropertyName == "ExternalClickedPin")
        {
            if (FormsControl.ExternalClickedPin != null)
            {
                var pin = Map.Pins.FirstOrDefault(x => x.MarkerId == FormsControl.ExternalClickedPin.MarkerId);
                if (pin != null)
                {
                    var field = this.GetType().BaseType.GetField("_markers", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);

                    var hiddenMarkers = (List<Marker>)field.GetValue(this);

                    var marker = hiddenMarkers.FirstOrDefault(x => x.Id == pin.MarkerId.ToString());
                    if (marker != null)
                    {
                        //simulating click
                        //1 invoke clicked event
                        // pin.SendMarkerClick(); // <- if needed for tests
                        //2 show info on map
                        marker.ShowInfoWindow(); //just what i was needing
                        //3 center pin on map
                        //Map.MoveToRegion(...); <- not here, im calling this from Forms subclassed control
                    }

                }
                FormsControl.ExternalClickedPin = null;
            }



        }
    }

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

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