Xcode 7 UI测试:如何在代码中解除一系列系统警报 [英] Xcode 7 UI Testing: how to dismiss a series of system alerts in code

查看:95
本文介绍了Xcode 7 UI测试:如何在代码中解除一系列系统警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用新的Xcode 7 UI测试功能编写UI测试用例。在我的应用程序的某些时刻,我要求用户允许摄像头访问和推送通知。因此会出现两个iOS弹出窗口:MyApp想要访问相机弹出窗口和MyApp想要发送通知弹出窗口。我希望我的测试可以解除两个弹出窗口。

I am writing UI test cases using the new Xcode 7 UI Testing feature. At some point of my app, I ask the user for permission of camera access and push notification. So two iOS popups will show up: "MyApp Would Like to Access the Camera" popup and "MyApp Would Like to Send You Notifications" popup. I'd like my test to dismiss both popups.

UI录制为我生成了以下代码:

UI recording generated the following code for me:

[app.alerts[@"cameraAccessTitle"].collectionViews.buttons[@"OK"] tap];

然而, [app.alerts [@cameraAccessTitle]存在] 解析为false,上面的代码生成错误:断言失败:UI测试失败 - 获取刷新快照失败错误Domain = XCTestManagerErrorDomain Code = 13复制属性时出错-25202

However, [app.alerts[@"cameraAccessTitle"] exists] resolves to false, and the code above generates an error: Assertion Failure: UI Testing Failure - Failure getting refresh snapshot Error Domain=XCTestManagerErrorDomain Code=13 "Error copying attributes -25202".

那么在测试中解除一堆系统警报的最佳方法是什么?系统弹出窗口会中断我的应用程序流并立即使我的正常UI测试用例失败。事实上,任何有关如何绕过系统警报的建议,以便我可以恢复测试通常的流程,这是值得赞赏的。

So what's the best way of dismissing a stack of system alerts in test? The system popups interrupt my app flow and fail my normal UI test cases immediately. In fact, any recommendations regarding how I can bypass the system alerts so I can resume testing the usual flow are appreciated.

此问题可能与此SO帖子有关,该帖子也没有答案: Xcode7 | Xcode UI测试|如何处理位置服务提醒?

This question might be related to this SO post which also doesn't have an answer: Xcode7 | Xcode UI Tests | How to handle location service alert?

提前致谢。

推荐答案

Xcode 7.1



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

位置对话框只是一个字符串,可以帮助开发人员识别访问哪个处理程序,它不是特定于警报的类型。

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.

我相信返回<来自处理程序的code> true 将其标记为完成,这意味着它不会再被调用。根据您的情况,我会尝试返回 false ,这样第二个警报将再次触发处理程序。

I believe that returning true from the handler marks it as "complete", which means it won't be called again. For your situation I would try returning false so the second alert will trigger the handler again.

以下内容将解除Xcode 7 Beta 6中的单个系统警报:

The following will dismiss a single "system alert" in Xcode 7 Beta 6:

let app = XCUIApplication()
app.launch()
// trigger location permission dialog

app.alerts.element.collectionViews.buttons["Allow"].tap()

Beta 6为UI测试引入了一系列修复程序,我相信这是其中之一他们。

Beta 6 introduced a slew of fixes for UI Testing and I believe this was one of them.

另请注意,我直接在 -alerts上调用 -element 。在 XCUIElementQuery 上调用 -element 会强制框架在屏幕上选择唯一的匹配元素。这适用于一次只能显示一个警报的警报。但是,如果您为标签尝试此操作并且有两个标签,则框架将引发异常。

Also note that I am calling -element directly on -alerts. Calling -element on an XCUIElementQuery forces the framework to choose the "one and only" matching element on the screen. This works great for alerts where you can only have one visible at a time. However, if you try this for a label and have two labels the framework will raise an exception.

这篇关于Xcode 7 UI测试:如何在代码中解除一系列系统警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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