xcode - Xcode7 | Xcode UI测试|如何处理位置服务警报?

标签 xcode xctest ios9 xcode7-beta4 xcode-ui-testing

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

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

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

我已记录以下内容:

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。例如:
  • 点按屏幕上的某个位置
  • 在应用程序外部获取警报

  • 那我该如何克服呢?有没有一种方法可以配置测试目标,因此不需要位置服务授权。

    最佳答案

    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作为参数

    Xcode 7.1

    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
    

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

    Xcode 7.0

    以下内容将消除Xcode 7 Beta 6中的单个“系统警报”:
    let app = XCUIApplication()
    app.launch()
    // trigger location permission dialog
    
    app.alerts.element.collectionViews.buttons["Allow"].tap()
    

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

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

    关于xcode - Xcode7 | Xcode UI测试|如何处理位置服务警报?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31746225/

    相关文章:

    iphone - 恢复到 git 中的特定提交,构建,然后恢复到最新的更改

    ios - LazyVGrid 中的 SwiftUI 中心项目

    xcode - 为什么 Storyboard安全区域在 iPhone X 上无法正常工作?

    ios - Xcode UI 测试 : how to identify a custom view with no static text?

    objective-c - 在 iOS 9 中是否有替代(已弃用的)canOpenURL 的方法?

    ios - 管理多个与 git 共享代码的 Xcode 项目

    ios - 如何在没有超时错误的情况下在 XCTest 中等待 T 秒?

    ios - UI : iOS 中动态数据的 UI 单元测试失败

    ios - 在 iOS 9 中设置 NSAllowsArbitraryLoads - Apple 会拒绝该应用程序吗?

    ios - 我的非ATS支持应用程序如何在iOS9上运行?