ios - Xcode 7 用户界面测试 : Dismiss Push and Location alerts

标签 ios xcode swift xcode-ui-testing

我遇到 Xcode 7 UI 测试问题。

该应用在我的用户登录后显示两个警报,请求位置 警报和推送通知 警报。这些通知一个接一个地显示。位置第一个出现。

我尝试自动关闭它们以开始我的测试。

为此,我添加了两个 UIInterruptionMonitor,第一个用于位置警报,第二个用于通知推送警报。

    addUIInterruptionMonitorWithDescription("Location Dialog") { (alert) -> Bool in
        /* Dismiss Location Dialog */
        if alert.collectionViews.buttons["Allow"].exists {
            alert.collectionViews.buttons["Allow"].tap()
            return true
        }
        return false
    }
    addUIInterruptionMonitorWithDescription("Push Dialog") { (alert) -> Bool in
        /* Dismiss Push Dialog */
        if alert.collectionViews.buttons["OK"].exists {
            alert.collectionViews.buttons["OK"].tap()
            return true
        }
        return false
    }

但只有 Location 被触发,Push Notifications 的处理程序 UIInterruptionMonitor 永远不会被调用。

如果我在 Request Location UIInterruptionMonitor 中返回 true 作为 this other post accepted answer指定。两个处理程序都被调用,但是 UIInterruptionMonitor 中的 alert 参数链接到 Request Location 警报 View ,因此永远找不到“确定”按钮。

如何关闭这两个连续的警报 View ?

最佳答案

虽然不理想,但我发现如果您只是等到一个授权对话框完成后再在应用中显示另一个授权对话框,UI 测试可以连续接收多个请求。

    if CLLocationManager.authorizationStatus() == .AuthorizedWhenInUse || CLLocationManager.authorizationStatus() == .AuthorizedAlways {
        self.locationManager.requestLocation()
    } else {
        self.contactStore.requestAccessForEntityType(.Contacts) { _ in
            self.locationManager.requestWhenInUseAuthorization()
        }
    }

我实际上是在我的代码中的不同位置请求访问联系人,但它可以很好地处理多个同时请求。

然后在我的测试中:

    addUIInterruptionMonitorWithDescription("Location Dialog") { (alert) -> Bool in
        let button = alert.buttons["Allow"]
        if button.exists {
            button.tap()
            return true
        }
        return false
    }
    addUIInterruptionMonitorWithDescription("Contacts Dialog") { (alert) -> Bool in
        let button = alert.buttons["OK"]
        if button.exists {
            button.tap()
            return true
        }
        return false
    }

    app.buttons["Location"].tap()

    app.tap() // need to interact with the app for the handler to fire
    app.tap() // need to interact with the app for the handler to fire

关于ios - Xcode 7 用户界面测试 : Dismiss Push and Location alerts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33875301/

相关文章:

iphone - Xcode——获取 force_load 以使用相对路径

ios - aws ios sdk - 从 s3 下载图像的问题

iphone - IOS中获取UITableView的行号

ios - 在 xcode 4.5 上 Symbolicate 崩溃失败

ios - 使用图像选择器将照片保存到 2 个不同的 imageview (swift 3)

ios - 用于具有动态高度的 View /标签的 Swift heightForRowAtIndexPath

ios - iOS Swift 上的 AWSS3TransferUtilityErrorDomain Code=2 尝试上传图像/pdf 时

ios - 在 parse.com 上存储消息传递应用程序的发送消息历史记录?

ios - 选择 TableView 中的每个单元格时呈现不同的 View

ios - Xcode 编译器或自动完成无法识别 Swift 类/协议(protocol)