我正在使用 Xcode7/iOS 9 中引入的 XCUIApplication、XCUIElement 和 XCUIElementQuery 为我的一个应用程序编写 UI 测试用例。
我遇到了路障。测试用例中的屏幕之一需要 iOS 的定位服务。正如预期的那样,系统会提示用户允许使用定位服务,并带有标题为:Allow “App name” to access your location while you use the app?
带有Allow
&Don't Allow
按钮的警报。
问题似乎是因为警报是由操作系统本身提出的,所以它不存在于应用程序的元素子树中。
我记录了以下内容:
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 录制也生成了类似的代码:
XCUIApplication().alerts["Allow “App Name” to access your location while you use the app?"].collectionViews.buttons["Allow"].tap()
我还没有找到任何可以让我解决这个问题的 API。例如:
- 点击屏幕上的某个位置
- 在应用程序外获取警报
那么我该如何克服呢?有没有办法配置测试目标,以便不需要位置服务授权。