iosmkannotationmkannotationviewuitestmkuserlocation

In iOS UI Testing, how do I identify the MKUserLocation annotation?


I'm struggling to identify how I can tell that the MKMapView is showing the current user location pin in a UI Test. This is a MKUserLocation annotation (and I've created an MKAnnotationView to use an image for the pin instead of the blue spot.)

Debugging tells me that the MKUserLocation annotation instance has a title of "My Location". I was expecting that the following would work:

app.maps.element.otherElements["My Location"]

would return it but [that code].exists returns false. Printing debugDescription of otherElements lists a whole set of annotations on the map but not the User Location.

Is there a different type of XCUIElement that represents the pin/annotation/annotation view that I can check for?


Solution

  • Use accessibility labels to access your MKAnnotationView. In your code, retrieve your user location annotation and set an accessibility label for it:

    yourAnnotation.accessibilityLabel = "labelYouWillUseToFindIt"
    

    Then during your tests you can do:

    app.maps.element.otherElements["labelYouWillUseToFindIt"]
    

    Use print(app.debugDescription) to see if your annotation is visible and if not, force it to be accessible with:

    yourAnnotation.isAccessibilityElement = true
    

    You can read more about accessibility labels here