处理位置服务警报的UITest案例

问题描述 投票:1回答:1

我正在为我的项目编写UI测试用例。

我的项目流程如下:

  • 登录屏幕。用户输入凭据并按登录。
  • 主屏幕。有用户许可的位置要求系统。我允许它。
  • 登出。

因此,当我全新安装应用程序时,此流程将记录在测试用例中,如果我执行新的构建,则可以正常工作。

但问题是,当我在旧版本上进行测试时,没有针对位置许可的警报,并且测试失败。每次运行测试时,我如何处理这种情况或询问用户是否允许?

为了重置用户的凭据,我将launchArguments传递给XCUIApplication()并在AppDelegate中处理。

我已经实现了代码,如果它的正确方法让我知道:

addUIInterruptionMonitor(withDescription: "Allow “APP” to access your location?") { (alert) -> Bool in
            alert.buttons["Only While Using the App"].tap()

            return true
        }

如果警报到来,上述代码适用于两者。

ios xcode xctest xcode-ui-testing ui-testing
1个回答
3
投票

使用中断监视器是正确的方法。但是,在与警报交互之前,检查显示的警报是否是您期望的警报更安全:

addUIInterruptionMonitor(withDescription: "Allow “APP” to access your location?") { (alert) -> Bool in
    let button = alert.buttons["Only While Using the App"]
    if button.exists {
        button.tap()
        return true // The alert was handled
    }

    return false // The alert was not handled
}
© www.soinside.com 2019 - 2024. All rights reserved.