5

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?

Community
  • 1
  • 1
Khant Thu Linn
  • 5,905
  • 7
  • 52
  • 120

2 Answers2

5

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

emoleumassi
  • 4,881
  • 13
  • 67
  • 93
  • `app.tap()` really makes sense – brigadir Oct 05 '17 at 15:20
  • why is app.tap() necessary? – Hasaan Ali Oct 13 '17 at 10:57
  • The interrupt handler will not fire until you attempt a touch action – ablarg Mar 30 '18 at 17:45
  • @ablarg Instead of app.tap() can it be any other view tap() ? Im using KIF in my UnitTest Target, so I dont have app.tap(), what can I use instead of that ?? – Maryam Fekri Apr 18 '18 at 19:32
  • Try tapping the origin of the frame (or any point / object you know will specifically not have a deleterious effect) – ablarg Apr 30 '18 at 16:45
  • @ablarg : I would be grateful if you can provide a proof of `The interrupt handler will not fire until you attempt a touch action`. I searched on developer forum as well as other stackoverflow posts.. but no where is a proof that it is necessary to use tap(). Please help thanks! – Gaurav Nov 14 '18 at 19:29
1

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
Hasaan Ali
  • 1,192
  • 16
  • 22
  • Using the NSPredicate to find the right button is very important. I could use this method to tap the Join button on Join Wi-Fi Network Dialog in UI Tests. Thanks. – Pankaj Kulkarni Aug 28 '20 at 14:53