Search code examples
swift3swift4xcode9xcuitest

Handle system dialog (alert) in XCUITest with Swift 3 / Swift 4 in Xcode 9


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.

Solution

  • 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
        }