How to access callout view on clicking on map annotation using swift in xctest

436 Views Asked by At

I want to access callout views and do some UIAutomation on those views. I'm able to click on map markers/annotations but not able to access the callout view. The following code used to tap on the marker:

let marker = app.otherElements.matching(identifier: "mapMarker").element(boundby: 0)
marker.tap();

After this, I'm getting the callout view of the respected marker/annotation. I need to access that callout. Please suggest me on this.

2

There are 2 best solutions below

3
Roman Zakharov On

You should create a breakpoint after the callout is snown, then type po print(app.debugDescription) (or simply po app in XCode 11) in lldb in order to view the whole hierarchy of UI elements.

Locate the needed element and access it further in code.

Also, consider rewriting your marker code in a shorter way:

let marker = app.otherElements["mapMarker"].firstMatch

Please notice firstMatch aborts search of elements after it found the first one.

Drop firstMatch, if you want to check that the element is unique

let marker = app.otherElements["mapMarker"]

0
Kimothy On

Same as Smart Monkey said, but to add more code based off the comment from ablarg:

Ex: "mapMarker" being the accessibility ID for the element

    let mapMarker = app.maps.otherElements["mapMarker"].firstMatch
    let mapMarkerExists = mapMarker.waitForExistence(timeout: 3)
    if mapMarkerExists {
      mapMarker.tap()
  }

waitForExistence(timeout:) returns a bool, so if the element appears before the timeout expires (it finds the element) take action (tap) on the element.

Make sure the element is enabled for accessibility and has the accessibility ID set.