27
private func acceptPermissionAlert() {
    
    _ = addUIInterruptionMonitor(withDescription: "") { alert -> Bool in
        
        if alert.buttons["Don’t Allow"].exists { //doesnt get here second time
            
            alert.buttons.element(boundBy: 1).tapWhenExists()
            
            return true
        }
        
        return false
    }
}

and this doesn't work for:

enter image description here

In the beginning of the app, it works perfect while accepting permission for notifications, but here, it doesn't work. Why is this?

halfer
  • 19,824
  • 17
  • 99
  • 186
Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358

3 Answers3

25

I'vs found that addUIInterruptionMonitor sometimes doesn't handle an alert in time, or until tests have finished. If it isn't working, try using Springboard, which manages the iOS home screen. You can access alerts, buttons, and more from there, and this is particularly useful for tests where you know exactly when an alert will show.

So, something like this:

let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard") 

let alertAllowButton = springboard.buttons.element(boundBy: 1)
if alertAllowButton.waitForExistence(timeout: 5) {
   alertAllowButton.tap()
}

The buttons.element(boundBy:1) will ensure you tap the button on the right, change 1 to 0 to tap the left, (because sometimes the ' in "Don't Allow" causes a problem).

mokagio
  • 16,391
  • 3
  • 51
  • 58
Taylor Lindsay
  • 251
  • 3
  • 4
23

Add:

app.tap()

at the end of the method.

This is because you need to interact with the app for the handler to fire.

nmr
  • 16,625
  • 10
  • 53
  • 67
Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
  • 5
    or app.swipeUp() if tapping would cause something to actually happen in your app (and you don't want it to) – xaphod Oct 19 '16 at 20:28
  • 2
    It works for me. This is kind of weird... "you need to interact with the app for the handler to fire." is that somewhere documented? – cornr Nov 30 '17 at 10:25
  • is there a radar for this bug? – Wladek Surala Jan 03 '18 at 11:21
  • 3
    `app.tap()` taps to the middle of the screen, which might result in unwanted actions, as @xaphod pointed out. If you cannot use `swipeUp()` either, maybe you can tap to a specific point on the screen, see [here](https://stackoverflow.com/a/36287500/1987726) – Reinhard Männer Feb 06 '18 at 11:06
  • Related : https://github.com/onmyway133/blog/issues/82#issue-262034541 – Axel Guilmin Mar 10 '20 at 14:56
  • Add this, but handler is still not triggered on Alert action sheet. – Zhou Haibo Feb 23 '22 at 03:12
  • Any interaction performed could trigger the handler, not just `app`. The key is: the handler would not be automatically triggered on the alert popping up. The handler will run only when the alert pops up AND after you fail to interact with some element. – Ting Yi Shih Mar 30 '22 at 07:32
12

After adding the interruption monitor, you should continue to interact with the app as if it has not appeared.

Also note that you have a 'smart quote' in your button identifier, instead of a regular apostrophe.

let photosAlertHandler = addUIInterruptionMonitor(withDescription: "Photo Permissions") { alert -> Bool in
    if alert.buttons["Don't Allow"].exists {
        alert.buttons.element(boundBy: 1).tapWhenExists()
        return true
    }
    return false
}

// Do whatever you want to do after dismissing the alert
let someButton = app.buttons["someButton"]
someButton.tap() // The interruption monitor's handler will be invoked if the alert is present

When the next interaction happens after the alert appears, the interruption monitor's handler will be invoked and the alert will be handled.

You should also remove the interruption monitor when you think you're done with it, otherwise it will be invoked for any other alerts that appear.

removeUIInterruptionMonitor(photosAlertHandler)
Oletha
  • 7,324
  • 1
  • 26
  • 46
  • 3
    The actual button has the 'smart quote' character `’` not `'`. Also your code produces the compiler error *Value of type 'XCUIElement' has no member 'tapWhenExists'*. – Aaron Brager May 27 '19 at 15:30
  • @AaronBrager the `tapWhenExists()` method is one from the question's code, not a standard library function. I suggest you use `tap()` instead, wrapped in a check for `exists`. – Oletha May 27 '19 at 16:14
  • "When the next interaction happens", upvote from me for that one – Simon Apr 28 '20 at 15:32
  • "When the next interaction happens" is indeed the key takeaway for me. – Amresh Feb 04 '21 at 09:33