XCode 7 UI 测试:关闭系统生成的 UIAlertController 不起作用 [英] XCode 7 UI Testing: Dismissal of system-generated UIAlertController does not work

查看:18
本文介绍了XCode 7 UI 测试:关闭系统生成的 UIAlertController 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UI 测试,其中涉及取消系统生成的 UIAlertController.此警报要求用户授予访问设备日历的权限.测试的目标是点击 OK 按钮后的行为:

I have a UI test which involves the dismissal of a system-generated UIAlertController. This alert asks the user for the permission to access the device's calendar. The objective of the test is the behaviour after a tap on the OK button:

1 let app = XCUIApplication()
...
// this code was basically generated by the recording feature of XCode 7 
2 app.alerts.elementBoundByIndex(0).collectionViews.buttons["OK"].tap()

现在,第 2 行不是点击 OK 按钮,而是让模拟器点击第一个按钮,它恰好是 Cancel 按钮......

Now, instead of clicking the OK button, line 2 makes the simulator tap onto the first button which happens to be the Cancel button...

另外,我发现测试框架不能准确识别出现的警报.因此,如果我检查当前的警报计数,我总是得到 0:

Additionally, I found out that the testing framework does not accurately recognize the appearing alert. So if I check the current count of alerts I always get 0:

// ...tap...
let count = app.alerts.count // == 0

如果我对条件使用 NSPredicate 并等待几秒钟,也会发生这种情况.

This also happens if I use an NSPredicate for the condition and wait for several seconds.

UI 测试是否可能无法与系统生成的警报一起可靠地工作?我使用的是 XCode 7.0.1.

Is it possible that UI tests do not work reliably with system-generated alerts? I am using XCode 7.0.1.

推荐答案

Xcode 7.1 终于解决了系统警报问题.但是,有两个小问题.

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

首先,您需要在显示警报之前设置一个UI Interuption Handler".这是我们告诉框架如何处理出现的警报的方式.

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

位置对话框"只是一个字符串,用于帮助开发人员识别访问了哪个处理程序,它并不特定于警报类型.我相信从处理程序返回 true 将其标记为完成",这意味着不会再次调用它.

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.

这篇关于XCode 7 UI 测试:关闭系统生成的 UIAlertController 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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