If you return false on the addUIInterruptionMonitor's handler, the framework will tap the cancel button ('userTestingAttributes CONTAINS "cancel-button"').
So, if you enters an addUIInterruptionMonitor you should not return false, fail the test saying you found an unexpected System Alert instead, like so:
XCTFail("Unexpected System Alert")
return false
(Before that fail I like manually taking a screenshot, that you definely help)
If you have more than on System Alert (Notification and Location, for example) you can either
- use one addUIInterruptionMonitor checking for the possible buttons and then, not remove that monitor
- or you can have multiple monitors and make sure to remove each after they are used (if you do not remove, only one monitor will be used).
The second option seems better, but imagine that your first test handles the first alert, but fails before handling the second, in this case, your second test will see the second alert only. If the buttons for the alert are different, the second monitor will not know how to handle it.
First case:
addUIInterruptionMonitor(withDescription: "alert monitor") { (alert) -> Bool in
let btnAllow = alert.buttons["Allow"]
let btnAllowAlways = alert.buttons["Always Allow"]
if btnAllow.exists {
btnAllow.tap()
return true
}
if btnAllowAlways.exists {
btnAllowAlways.tap()
return true
}
XCTFail("Unexpected System Alert")
return false
}
Second case:
locationDialogHandeler = addUIInterruptionMonitor(withDescription: "locationDialogHandeler") { (alert) -> Bool in
if btnAllowAlways.exists {
NotificationCenter.default.post(name: self.notificationNotificationDialogHander, object: nil)
btnAllowAlways.tap()
return true
}
XCTFail("Unexpected System Alert")
return false
}
NotificationCenter.default.addObserver(forName: notificationLocationDialogHander, object: nil, queue: nil) { (notification) in
if let locationDialogHandeler = self.locationDialogHandeler {
self.removeUIInterruptionMonitor(locationDialogHandeler)
}
}
//...
Remember that you should add UIInterruptionMonitor in reverse order. Therefore, in my case, since Location Alert appears first, that's the last monitor I add.
P.S: I add the monitors even before launching the app (app.launch())