关闭位置服务请求对话框

dismiss location services request dialog

在我的 UI 测试开始时,我有

addUIInterruptionMonitor(withDescription: "Location Dialog") { (alert) -> Bool in
    let button = alert.buttons["Allow"]
    if button.exists {
        snapshot("request location service")
        button.tap()
        return true
    }
    return false
}

这应该关闭位置服务请求对话框,但它什么也不做,也永远不会到达处理程序。我也曾尝试在 setUp() 中设置此代码,但它也没有用。

我认为问题可能出在应用程序中发生的第一件事是正在显示对话框,这可能为时过早(可能发生在 addUIInterruptionMonitor 被调用之前)

我该如何解决这个问题?

您必须在添加 UIInterruptionMonitor 后立即与应用程序交互。这可以是一个简单的点击:

addUIInterruptionMonitor(withDescription: "Location Dialog") { (alert) -> Bool in
   let button = alert.buttons["Allow"]
   if button.exists {
   button.tap()
      return true
   }
   return false
}
// interact with the app
app.tap()

如果 app.tap() 干扰了你的测试你也可以使用 app.swipeUp()

请注意,位置服务权限对话框在 iOS11 中发生了变化。现在有 3 个按钮,因此您必须使用 alert.buttons["Always Allow"] 关闭对话框。