addUIInterruptionMonitor(with description:handler:)无法在iOS 10或9上运行
以下测试在iOS 11上运行良好。它将取消请求使用位置服务权限的警报,然后放大地图。在iOS 10或9上,它不执行任何操作,测试仍然成功addUIInterruptionMonitor(with description:handler:)无法在iOS 10或9上运行,ios,swift,xctest,xcode-ui-testing,Ios,Swift,Xctest,Xcode Ui Testing,以下测试在iOS 11上运行良好。它将取消请求使用位置服务权限的警报,然后放大地图。在iOS 10或9上,它不执行任何操作,测试仍然成功 func testExample() { let app = XCUIApplication() var handled = false var appeared = false let token = addUIInterruptionMonitor(withDescription: "Location") { (aler
func testExample() {
let app = XCUIApplication()
var handled = false
var appeared = false
let token = addUIInterruptionMonitor(withDescription: "Location") { (alert) -> Bool in
appeared = true
let allow = alert.buttons["Allow"]
if allow.exists {
allow.tap()
handled = true
return true
}
return false
}
// Interruption won't happen without some kind of action.
app.tap()
removeUIInterruptionMonitor(token)
XCTAssertTrue(appeared && handled)
}
有人知道原因和/或解决方法吗
下面是一个项目,您可以在其中重现该问题:
更新
Xcode 9.3测试版的变更日志显示以下内容
XCEST UI中断监视器现在可以在运行iOS 10的设备和模拟器上正常工作。(33278282)
将.exists
更新为.waitForExistence(超时:10)
,详细信息请查看评论。我遇到了这个问题,为我工作
请注意,这不是让UIInterruptionMonitor工作的修复方法,而是解除警报的另一种方式。您也可以删除addUIInterruptionMonitor设置。您需要让springboard.button[“Allow”]存在
测试权限警报可能出现的任何位置。若可能的话,强制它在测试的早期阶段出现,这样以后就不必再担心它了
令人高兴的是,springboard.buttons[“Allow”]存在
code在iOS 11中仍然有效,因此您可以有一个代码路径,而不必为iOS 10做一件事,为iOS 11做另一件事
顺便说一句,我将基本问题(addUIInterruptionMonitor在iOS 11之前不工作)记录为Apple的一个bug。现在它已经作为副本关闭,所以我想他们承认这是一个bug。我使用了,它比中断的工作得更好。
如果您决定使用该功能,我强烈建议您使用服务员功能。我创建这个是为了等待任何类型的XUIElement出现:
试试看
// function to wait for an ui element to appear on screen, with a default wait time of 20 seconds
// XCTWaiter was introduced after Xcode 8.3, which is handling better the timewait, it's not failing the test. It uses an enum which returns: 'Waiters can be used with or without a delegate to respond to events such as completion, timeout, or invalid expectation fulfilment.'
@discardableResult
func uiElementExists(for element: XCUIElement, timeout: TimeInterval = 20) -> Bool {
let expectation = XCTNSPredicateExpectation(predicate: NSPredicate(format: "exists == true"), object: element)
let result = XCTWaiter().wait(for: [expectation], timeout: timeout)
guard result == .completed else {
return false
}
return true
}
我真不敢相信这能奏效:)谢谢!然而,我宁愿按索引访问按钮,否则它在不同的iOS版本(在iOS 11中更改了按钮标题)中不起作用。使用
addUIInteruptionMonitor()
无法消除SFAuthenticationSession
警报,但这种技术对我们有效。这对我测试tel:
URL方案很有效AddUIInterruptionMonitor
找不到iOS生成的警报。这对我来说不是现成的。但是当我用.waitForExistence(超时时间:10)
替换.exists
时,它成功了。@ChristianBeer谢谢分享。是的,我们也有同样的问题。似乎.exist
在最新Xcode中真正的UI出现在屏幕上之前返回true.waitForExistence
在这种情况下确实有所帮助。
// function to wait for an ui element to appear on screen, with a default wait time of 20 seconds
// XCTWaiter was introduced after Xcode 8.3, which is handling better the timewait, it's not failing the test. It uses an enum which returns: 'Waiters can be used with or without a delegate to respond to events such as completion, timeout, or invalid expectation fulfilment.'
@discardableResult
func uiElementExists(for element: XCUIElement, timeout: TimeInterval = 20) -> Bool {
let expectation = XCTNSPredicateExpectation(predicate: NSPredicate(format: "exists == true"), object: element)
let result = XCTWaiter().wait(for: [expectation], timeout: timeout)
guard result == .completed else {
return false
}
return true
}