iOS UITesting:使用addUIInterruptionMonitorWithDescription自动处理所有系统提示

时间:2016-04-17 04:57:41

标签: ios xcode xcode-ui-testing

我已经读完了这两本。

Xcode7 | Xcode UI Tests | How to handle location service alert?

Xcode 7 UI Testing: Dismiss Push and Location alerts

我可以知道以下内容吗?

1)对于位置,放置“位置对话框”表示它将处理位置提示。它如何识别?

2)如何处理访问照片库或相机的系统提示?是否有任何处理程序描述列表?

2 个答案:

答案 0 :(得分:5)

这里是addUIInterruptionMonitorWithDescription的xcode文档。

/*! Adds a handler to the current context. Returns a token that can be used to unregister the handler. Handlers are invoked in the reverse order in which they are added until one of the handlers returns true, indicating that it has handled the alert.
     @param handlerDescription Explanation of the behavior and purpose of this handler, mainly used for debugging and analysis.
     @param handler Handler block for asynchronous UI such as alerts and other dialogs. Handlers should return true if they handled the UI, false if they did not. The handler is passed an XCUIElement representing the top level UI element for the alert.
     */
    public func addUIInterruptionMonitorWithDescription(handlerDescription: String, handler: (XCUIElement) -> Bool) -> NSObjectProtocol

1)"位置对话框"只是一个处理程序描述,您可以识别您处理的警报。你可以写别的东西。

2)你必须使用相同的方法。只需点按应用后即可。

在这里,我使用这部分代码来处理推送通知:

addUIInterruptionMonitorWithDescription("Push notifications") { (alert) -> Bool in
       if alert.buttons["OK"].exists {
            alert.buttons["OK"].tap()
            return true
       }
       return false
}
app.tap()

干杯

答案 1 :(得分:0)

在xcode 9.1上,仅在测试设备具有iOS 11 时才会处理警报。不适用于较旧的iOS版本,例如10.3等。参考:https://forums.developer.apple.com/thread/86989

要处理警报,请使用:

//Use this before the alerts appear. I am doing it before app.launch()

let allowButtonPredicate = NSPredicate(format: "label == 'Always Allow' || label == 'Allow'")
//1st alert
_ = addUIInterruptionMonitor(withDescription: "Allow to access your location?") { (alert) -> Bool in
    let alwaysAllowButton = alert.buttons.matching(allowButtonPredicate).element.firstMatch
    if alwaysAllowButton.exists {
        alwaysAllowButton.tap()
        return true
    }
    return false
}
//Copy paste if there are more than one alerts to handle in the app