Handler of addUIInterruptionMonitor is not called for Alert related to Photos

你。 提交于 2019-12-05 15:57:00

问题


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 doesnt work for:

In the beginning of the app, it works perfect while acepting permission for notifications, but here... doesnt work.

Do you know why?


回答1:


Add:

app.tap()

at the end of the method.

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




回答2:


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)



回答3:


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).



来源:https://stackoverflow.com/questions/39973904/handler-of-adduiinterruptionmonitor-is-not-called-for-alert-related-to-photos

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!