Tah*_*mad 31 xcode xctest ios9 xcode-ui-testing xcode7-beta4
我正在使用Xcode7/iOS 9中引入的XCUIApplication,XCUIElement和XCUIElementQuery为我的应用程序编写UI测试用例.
我遇到了路障.测试用例中的一个屏幕需要iOS的位置服务.正如预期的那样,系统会提示用户允许使用名为" Allow “App name” to access your location while you use the app?
with Allow
&Don't Allow
buttons"的警报来使用位置服务.
问题是或许似乎由于警报由OS本身呈现,因此它不存在于Application的元素子树中.
我记录了以下内容:
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
Run Code Online (Sandbox Code Playgroud)
甚至UI录制生成类似的代码:
XCUIApplication().alerts["Allow “App Name” to access your location while you use the app?"].collectionViews.buttons["Allow"].tap()
Run Code Online (Sandbox Code Playgroud)
我还没有找到任何可以让我解决这个问题的API.例如:
那我该怎么办呢?有没有办法配置测试目标,以便不需要位置服务授权.
Joe*_*tti 32
let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")
let allowBtn = springboard.buttons["Allow"]
if allowBtn.exists {
allowBtn.tap()
}
Run Code Online (Sandbox Code Playgroud)
_ = 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
Run Code Online (Sandbox Code Playgroud)
请注意,它有点不同,因为方法名称现在是addUIInterruptionMonitor并将withDescription作为参数
Xcode 7.1最终修复了系统警报的问题.然而,有两个小陷阱.
首先,您需要在显示警报之前设置"UI Interuption Handler".这是告诉框架如何在出现警报时处理警报的方式.
其次,在显示警报后,您必须与界面进行交互.只需点击应用程序就可以正常工作,但这是必需的.
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
Run Code Online (Sandbox Code Playgroud)
"位置对话框"只是一个字符串,可帮助开发人员识别访问哪个处理程序,而不是特定于警报类型.
以下将解除Xcode 7 Beta 6中的单个"系统警报":
let app = XCUIApplication()
app.launch()
// trigger location permission dialog
app.alerts.element.collectionViews.buttons["Allow"].tap()
Run Code Online (Sandbox Code Playgroud)
Beta 6为UI测试引入了一系列修复,我相信这是其中之一.
还要注意我-element
直接打电话-alerts
.调用-element
上的XCUIElementQuery
力的框架,选择"唯一"匹配屏幕上的元素.这适用于一次只能显示一个警报的警报.但是,如果您为标签尝试此操作并且有两个标签,则框架将引发异常.
这是唯一对我有用的东西.使用Xcode 9 fwiw.
也可能与我已经addUIInterruptionMonitor
用于不同警报的相关.我尝试重新排序它们并没有什么区别.当你有两个时,可能是9中的问题,或者可能是我错误地使用它们.无论如何,下面的代码都有效.:)
let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")
let allowBtn = springboard.buttons["Allow"]
if allowBtn.exists {
allowBtn.tap()
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
11267 次 |
最近记录: |