XCUITesting许可弹出:出现警报,但UIInterruptionMonitor不火
问题描述:
我想编写一个测试,像这样:XCUITesting许可弹出:出现警报,但UIInterruptionMonitor不火
当我的应用程序进入到一定的窗格中,应该要求使用相机的权限。
我想测试窗格是否出现。我正在使用XC的内置UITest框架来执行此操作。根据我在谷歌和这里找到的,似乎我应该这样做:
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)")
}
测试从失败开始,很好。我实现了goToCameraPage,它正确地导致出现“授予权限”弹出窗口。不过,我希望这会触发中断监视器。然而,没有这样的中断被捕获,并且不会发生履行。
我在对话框出现后的某个地方阅读你应该做的app.tap()
。但是,当我这样做时,它会点击“允许”按钮。该对话框消失,仍然没有中断处理。
是否有某种权限对话框不被视为“警报”或无法处理?我甚至进去用一个看起来在app.alerts
的东西代替了中断位,但结果是空的,即使我正好看着模拟器中的弹出窗口。
谢谢!我正在使用iPhone 6s的Xcode7.2,iOS 9.2模拟器。
答
我也注意到了这个问题。看起来中断处理程序是异步运行的,无法断言它们是否被调用。等待期望似乎也会阻止中断监视器运行。看起来系统正在等待期望完成,期望等待中断监视器触发。经典的死锁案例。
然而,我发现,使用基于NSPredicate
一个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)")
}
显然,做XCUIApplication().tap()
内的谓词块某种程度上允许运行中断监控,即使测试用例正在等待期待。
我希望这对你的工作,因为它为我做!
我不喜欢这种方法,但又回到了类似的问题,我没有得到这个工作。 – Quintana
我也不喜欢它:D – pancake