I'm working on a UI unit test for my app, and am trying to figure out how to automatically have the test framework click "OK" when the system presents an alert asking for permission to access contacts.
So far, I've looked at these four SO posts, and tried the various suggestions, but am still unable to get this to work:
XCTest app tests and permissions alerts
Xcode 7 UI Testing: how to dismiss a series of system alerts in code
Xcode UI Testing allow system alerts series
Xcode 7 UI Testing: Dismiss Push and Location alerts
Here's what I currently am trying - however, the permissions dialog is still not being automatically accepted; the test waits for me to click "OK" before moving forward: func testApp() {
self.addUIInterruptionMonitor(withDescription: "MyDescription", handler: { (alert) -> Bool in
let button = alert.buttons["OK"]
if button.exists {
button.tap()
return true
}
return false
})
let app = XCUIApplication()
app.launch()
...
app.tap()
...
}
EDIT: Here's the change I've made based on @ad-johnson's suggestion:
var app: XCUIApplication!
override func setUp() {
super.setUp()
continueAfterFailure = false
app = XCUIApplication()
addUIInterruptionMonitor(withDescription: "Contact Auth")
{ (alert) -> Bool in if alert.buttons["OK"].exists {
alert.buttons["OK"].tap()
}
return true }
app.launch()
}
func testScreenshots() {
app.tap()
...
}