Xcode7 | Xcode UI测试|如何处理位置服务警报? [英] Xcode7 | Xcode UI Tests | How to handle location service alert?

查看:150
本文介绍了Xcode7 | Xcode UI测试|如何处理位置服务警报?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Xcode7/iOS 9中引入的XCUIApplication,XCUIElement和XCUIElementQuery为我的一个应用程序编写UI测试用例.

I am writing UI Test Cases for one one of my app using the XCUIApplication, XCUIElement and XCUIElementQuery introduced in Xcode7/iOS 9.

我遇到了路障.测试用例中的屏幕之一需要iOS的位置服务.如预期的那样,系统提示用户有关允许使用位置服务的提示为Allow App name to access your location while you use the app?Allow&的警报. Don't Allow按钮.

I have hit a road block. One of the screens in test case requires iOS's Location Services. As expected the user is prompted about allowing use of location service with alert titled: Allow "App name" to access your location while you use the app? with Allow & Don't Allow buttons.

问题大概是,由于警报是由OS本身提供的,因此它不在应用程序的元素子树中.

Problem is or so it seems that since the alert is presented by OS itself it is not present in Application's element sub-tree.

我记录了以下内容:

print("XYZ:\(app.alerts.count)")//0
var existence = app.staticTexts["Allow "App Name" to access your location while you use the app?"].exists
print("XYZ:\(existence)")//false
existence  = app.buttons["Allow"].exists
print("XYZ:\(existence)") //false

即使UI记录也会生成类似的代码:

Even UI recording generated similar code:

XCUIApplication().alerts["Allow "App Name" to access your location while you use the app?"].collectionViews.buttons["Allow"].tap()

我还没有找到能让我克服此问题的API.例如:

I have not found any API that can get me past this problem. For example:

  • 点击屏幕上的某个位置
  • 在应用外部获取警报

那么我该如何克服呢?有没有一种方法可以配置测试目标,从而不需要位置服务授权.

So how can I get past this? Is there a way to configure Test Targets so that Location Service Authorization is not required.

推荐答案

Xcode 9

    let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")
    let allowBtn = springboard.buttons["Allow"]
    if allowBtn.exists {
        allowBtn.tap()
    }

Xcode 8.3.3

    _ = addUIInterruptionMonitor(withDescription: "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


请注意,这有点不同,因为方法名称现在是addUIInterruptionMonitor,并以withDescription作为参数


Note that it is a bit different as the method name now is addUIInterruptionMonitor and takes withDescription as an argument

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

Xcode 7.1 has finally fixed a 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.

以下内容将消除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.

这篇关于Xcode7 | Xcode UI测试|如何处理位置服务警报?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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