Saintz
Saintz

Reputation: 69

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.

Upvotes: 1

Views: 2302

Answers (1)

MaewBot
MaewBot

Reputation: 66

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
    }

Upvotes: 5

Related Questions