XCode 7 UI Testing: Dismissal of system-generated UIAlertController does not work

坚强是说给别人听的谎言 提交于 2019-12-01 10:11:57

Xcode 7.1 has finally fixed the issue with system alerts. There are, however, two small gotchas.

First, you need to set up a "UI Interuption Handler" before presenting the alert. This is our way of telling the framework how to handle an alert when it appears.

Second, after presenting the alert you must interact with the interface. Simply tapping the app works just fine, but is required.

addUIInterruptionMonitorWithDescription("Location Dialog") { (alert) -> Bool in
    alert.buttons["Allow"].tap()
    return true
}

app.buttons["Request Location"].tap()
app.tap() // need to interact with the app for the handler to fire

The "Location Dialog" is just a string to help the developer identify which handler was accessed, it is not specific to the type of alert. I believe that returning true from the handler marks it as "complete", which means it won't be called again.

I managed to dismiss access prompt for contacts like this:

let alert = app.alerts["\u{201c}******\u{201d} Would Like to Access Your Contacts"].collectionViews.buttons["OK"]
if alert.exists
{
    alert.tap()
}

Replace asterisks with your app's name. It might work the same for calendar.

This is what I ended up using to get the prompt to appear and then allow access to Contacts:

func allowAccessToContacts(textFieldsName: String)
{
    let app = XCUIApplication()

    let textField = app.textFields[textFieldsName]
    textField.tap()
    textField.typeText("aaa")

    let alert = app.alerts["\u{201c}******\u{201d} Would Like to Access Your Contacts"].collectionViews.buttons["OK"]
    if alert.exists
    {
        alert.tap()
    }
    textField.typeText("\u{8}\u{8}\u{8}")
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!