Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 898
Khant Thu Linn Avatar asked Apr 17 '16 04:04

Khant Thu Linn


2 Answers

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

like image 182
emoleumassi Avatar answered Nov 15 '22 04:11

emoleumassi


On xcode 9.1, alerts are only being handled if the test device has iOS 11. Doesn't work on older iOS versions e.g 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
like image 33
Hasaan Ali Avatar answered Nov 15 '22 05:11

Hasaan Ali