Xcode 7 UI测试:关闭推送和位置警报 [英] Xcode 7 UI Testing: Dismiss Push and Location alerts

查看:133
本文介绍了Xcode 7 UI测试:关闭推送和位置警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了Xcode 7 UI测试的问题。



我的用户登录后,该应用会显示两条提醒,请求位置提醒以及推送通知提醒。这些通知一个接一个地显示。位置1首先显示。



我尝试自动解除它们以开始我的测试。



为了为此,我添加了两个 UIInterruptionMonitor ,第一个用于位置警报,第二个用于通知推送警报。

  addUIInterruptionMonitorWithDescription(Location Dialog){(alert) - > Bool in 
/ * Dismiss Location Dialog * /
if alert.collectionViews.buttons [Allow]。exists {
alert.collectionViews.buttons [Allow]。tap()
返回true
}
返回false
}
addUIInterruptionMonitorWithDescription(Push Dialog){(alert) - > Bool in
/ * Dismiss Push Dialog * /
if alert.collectionViews.buttons [OK]。exists {
alert.collectionViews.buttons [OK]。tap()
返回true
}
返回false
}

但是只触发了Location 1,推送通知的处理程序 UIInterruptionMonitor 永远不会被调用。



如果我在请求中返回true位置 UIInterruptionMonitor 这个其他帖子接受的答案指定。两个处理程序都被调用,但 UIInterruptionMonitor 中的 alert 参数链接到请求位置警报视图,因此永远找不到确定按钮。 / p>

如何解除这两个连续的警报视图?

解决方案

不太理想,我发现,如果你只是等到一个授权对话框完成后再在应用程序中显示另一个授权对话框,UI测试就可以连续获取多个请求。

 如果CLLocationManager.authorizationStatus()== .AuthorizedWhenInUse || CLLocationManager.authorizationStatus()== .AuthorizedAlways {
self.locationManager.requestLocation()
} else {
self.contactStore.requestAccessForEntityType(.Contacts){_ in
self。 locationManager.requestWhenInUseAuthorization()
}
}

我实际上是在请求访问权限在我的代码中的其他地方的联系人,但它可以处理多个同时请求就好了。



然后在我的测试中:

  addUIInterruptionMonitorWithDescription(Location Dialog){(alert) - > Bool in 
let button = alert.buttons [Allow]
if button.exists {
button.tap()
return true
}
return false
}
addUIInterruptionMonitorWithDescription(Contacts Dialog){(alert) - > Bool in
let button = alert.buttons [OK]
if button.exists {
button.tap()
return true
}
return false
}

app.buttons [Location]。tap()

app.tap()//需要与应用程序进行交互处理器来解雇
app.tap()//需要与应用程序进行交互以使处理程序解雇


I encountered a problem with Xcode 7 UI Testing.

The app displays two alerts after my user logs in, the Request Location Alert and the Push Notifications Alert. Those notifications are shown one right after the other. The Location one appears first.

I try to dismiss them automatically to start my tests.

In order to do that, I add two UIInterruptionMonitor, the first one for the Location Alert and the second one for the Notification Push Alert.

    addUIInterruptionMonitorWithDescription("Location Dialog") { (alert) -> Bool in
        /* Dismiss Location Dialog */
        if alert.collectionViews.buttons["Allow"].exists {
            alert.collectionViews.buttons["Allow"].tap()
            return true
        }
        return false
    }
    addUIInterruptionMonitorWithDescription("Push Dialog") { (alert) -> Bool in
        /* Dismiss Push Dialog */
        if alert.collectionViews.buttons["OK"].exists {
            alert.collectionViews.buttons["OK"].tap()
            return true
        }
        return false
    }

But only the Location one is triggered, the handler of Push Notifications UIInterruptionMonitor is never called.

If I return true in the Request Location UIInterruptionMonitor as this other post accepted answer specifies. Both handler are called but the alert parameter in both UIInterruptionMonitor links to the Request Location Alert View so the "OK" button is never found.

How can I dismiss those two successive alerts views?

解决方案

While not ideal, I found that if you simply wait until one authorization dialog has finished before presenting another one in the app, UI tests can pick up multiple requests in a row.

    if CLLocationManager.authorizationStatus() == .AuthorizedWhenInUse || CLLocationManager.authorizationStatus() == .AuthorizedAlways {
        self.locationManager.requestLocation()
    } else {
        self.contactStore.requestAccessForEntityType(.Contacts) { _ in
            self.locationManager.requestWhenInUseAuthorization()
        }
    }

I'm actually requesting access to contacts in a different place in my code, but it can handle multiple simultaneous requests just fine.

Then in my test:

    addUIInterruptionMonitorWithDescription("Location Dialog") { (alert) -> Bool in
        let button = alert.buttons["Allow"]
        if button.exists {
            button.tap()
            return true
        }
        return false
    }
    addUIInterruptionMonitorWithDescription("Contacts Dialog") { (alert) -> Bool in
        let button = alert.buttons["OK"]
        if button.exists {
            button.tap()
            return true
        }
        return false
    }

    app.buttons["Location"].tap()

    app.tap() // need to interact with the app for the handler to fire
    app.tap() // need to interact with the app for the handler to fire

这篇关于Xcode 7 UI测试:关闭推送和位置警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆