I want to write a XCUITest where I get a system dialog / alert. How can I handle it in Swift 3 / Swift 4 with Xcode 9? It should find the alert and click one of the two displayed buttons. Every time, if I search for the alert, the system can't find them.
XCUIApplication().alerts.element.exists // Will get nothing
XCUIApplication().alerts.element.buttons.element(boundBy: 0) // Will get nothing too.
You can try using addUIInterruptionMonitor. This example below is to tap button Allow on system alert dialog box when it appears on the screen.
addUIInterruptionMonitor(withDescription: "System alert") { (alert) -> Bool in
if alert.buttons["Allow"].exists {
alert.buttons["Allow"].tap()
}
return true
}