简体   繁体   中英

iOS UITesting : Handle all system prompt automatically with addUIInterruptionMonitorWithDescription

I have read through this two.

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

Xcode 7 UI Testing: Dismiss Push and Location alerts

Could I know the following?

1) For location, putting "Location Dialog" indicate that it gonna handle for location prompt. How does it recognise?

2) How to handle system prompt for accessing photo library or camera? Is there any list for handler description?

here the xcode-documentation of addUIInterruptionMonitorWithDescription .

/*! 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) "Location Dialog" is just a handlerDescription for you to identifie what alert you handle. You can write somethings else.

2) You have to use the same method. Just tap the app after.

Here i use this part of code to handle Push notifications:

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

Cheers

On xcode 9.1, alerts are only being handled if the test device has iOS 11 . Doesn't work on older iOS versions eg 10.3 etc. Reference: https://forums.developer.apple.com/thread/86989

To handle alerts use this:

//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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM