在 Xcode 9 中使用 Swift 3 / Swift 4 在 XCUITest 中处理系统对话框(警报)

Sai*_*ntz 1 swift3 swift4 xcode9 xcuitest

我想编写一个 XCUITest 来获得系统对话框/警报。如何使用 Xcode 9 在 Swift 3 / Swift 4 中处理它?它应该找到警报并单击两个显示的按钮之一。每次,如果我搜索警报,系统都找不到它们。

XCUIApplication().alerts.element.exists // Will get nothing
XCUIApplication().alerts.element.buttons.element(boundBy: 0) // Will get nothing too.
Run Code Online (Sandbox Code Playgroud)

小智 5

您可以尝试使用 addUIInterruptionMonitor。下面这个例子是在系统警报对话框出现在屏幕上时点击按钮允许。

    addUIInterruptionMonitor(withDescription: "System alert") { (alert) -> Bool in
        if alert.buttons["Allow"].exists {
            alert.buttons["Allow"].tap()
        }
        return true
    }
Run Code Online (Sandbox Code Playgroud)