用于权限弹出的 XCUITesting:出现警报,但 UIInterruptionMonitor 不会触发 [英] XCUITesting for permission popup: alert appears, but UIInterruptionMonitor does not fire

查看:14
本文介绍了用于权限弹出的 XCUITesting:出现警报,但 UIInterruptionMonitor 不会触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个这样的测试:

当我的应用进入某个窗格时,它应该请求使用相机的权限.

我想测试窗格是否出现.我正在使用 XC 的内置 UITest 框架来做到这一点.根据我在谷歌和这里找到的内容,我似乎应该执行以下操作:

let dialogAppearedExpectation =expectationWithDescription("Camera Permission Dialog Appears")addUIInterruptionMonitorWithDescription("Camera Permission Alert") { (alert) ->布尔输入dialogAppearedExpectation.fulfill()返回真}goToCameraPage()waitForExpectationsWithTimeout(10) {(错误:NSError?)->作废打印(错误:\(错误?.localizedDescription)")}

测试以失败开始,太好了.我实现了 goToCameraPage,它正确地导致授予权限"弹出窗口出现.但是,我希望这会触发中断监视器.然而,没有捕捉到这样的中断,也没有实现.

我在某处读到你应该在对话框出现后执行 app.tap().但是,当我这样做时,它会单击允许"按钮.对话框消失,仍然没有中断处理.

是否有某种方式可以使权限对话框不被视为警报"或无法处理?我什至进入并用一个只查看 app.alerts 的东西替换了中断位,但结果是空的,即使我正看着模拟器中的弹出窗口.>

谢谢!我使用的是 iPhone 6s 的 Xcode7.2、iOS 9.2 模拟器.

解决方案

我也注意到了这个问题.中断处理程序似乎是异步运行的,并且无法断言它们是否被调用.同样等待一个期望似乎完全阻止了中断监视器的运行.看起来系统正在等待期望实现,而期望正在等待中断监视器触发.一个经典的死锁案例.

然而,我发现了一个相当古怪的解决方案,它使用基于 NSPredicate 的期望:

var didShowDialog = false期望(对于:NSPredicate(){(_,_)在XCUIApplication().tap()//这是让它工作的魔法点击返回 didShowDialog},evaluateWith: NSNull(), handler: nil)addUIInterruptionMonitor(withDescription: "Camera Permission Alert") { (alert) ->布尔输入alert.buttons.element(boundBy: 0).tap()//不确定是 allow = 0 还是 1didShowDialog = 真返回真}goToCameraPage()waitForExpectations(timeout: 10) { (error: Error?) ->作废打印(错误:\(错误?.localizedDescription)")}

显然,在谓词块内执行 XCUIApplication().tap() 以某种方式允许中断监视器运行,即使测试用例正在等待预期.

我希望这对你和我一样有效!

I would like to write a test like so:

When my app goes to a certain pane, it should request permission to use the camera.

I want to test whether or not the pane appears. I am using XC's builtin UITest framework to do this. Per what I found on google and here, it seems like I should do the following:

let dialogAppearedExpectation = expectationWithDescription("Camera Permission Dialog Appears")

addUIInterruptionMonitorWithDescription("Camera Permission Alert") { (alert) -> Bool in
    dialogAppearedExpectation.fulfill()
    return true
}

goToCameraPage()

waitForExpectationsWithTimeout(10) { (error: NSError?) -> Void in
    print("Error: \(error?.localizedDescription)")
}

The test began with failing, great. I implemented goToCameraPage, which correctly causes the "give permission" popup to appear. However, I would expect this to trigger the interruption monitor. No such interruption is caught, however, and fulfillment does not occur.

I read somewhere that you should do app.tap() after the dialog appears. However, when I do that, it clicks the "allow" button. The dialog disappears and still no interruption is handled.

Is there some way in which permission dialogs are not considered "alerts" or can't be handled? I even went in and replaced the interruption bit with a thing which just looks at app.alerts, but that turns out to be empty, even as I'm looking right at the popup in Simulator.

Thanks! I am using Xcode7.2, iOS 9.2 simulator for iPhone 6s.

解决方案

I have noticed this problem as well. It seems like the interruption handlers are run asynchronously and there is no way to assert whether they were called. Also waiting for an expectation seems to prevent the interruption monitor from running at all. It looks like the system is waiting for the expectation to fulfil and the expectation is waiting for the the interruption monitor to fire. A classic case of deadlock.

However, I have found a rather quirky solution that uses NSPredicate-based expecations:

var didShowDialog = false
expectation(for: NSPredicate() {(_,_) in
    XCUIApplication().tap() // this is the magic tap that makes it work
    return didShowDialog
}, evaluatedWith: NSNull(), handler: nil)

addUIInterruptionMonitor(withDescription: "Camera Permission Alert") { (alert) -> Bool in
    alert.buttons.element(boundBy: 0).tap() // not sure if allow = 0 or 1
    didShowDialog = true
    return true
}

goToCameraPage()

waitForExpectations(timeout: 10) { (error: Error?) -> Void in
    print("Error: \(error?.localizedDescription)")
}

Apparently, doing the XCUIApplication().tap() inside the predicate block somehow allows the interruption monitor to be run, even though the test case is waiting for an expectation.

I hope this works as well for you as it did for me!

这篇关于用于权限弹出的 XCUITesting:出现警报,但 UIInterruptionMonitor 不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆