要在UITest期间处理系统提醒您必须添加一个UI中断监视器:
func testPhotoLibraryAccess() {
let app = XCUIApplication()
app.launch()
// when system alert is shown -> dismiss it by pressing "OK"
// (the description parameter is only there for debugging purposes
// so it can be anything you like)
addUIInterruptionMonitor(withDescription: "Photos Access Alert") { (alert) -> Bool in
alert.buttons["OK"].tap()
return true
}
// tap button that tries to open user's photo library
app.buttons["Open Photos"].tap()
// select "Moments"
app.buttons["Moments"].tap()
XCTAssert(app.navigationBars["Moments"].exists)
}
为了使这项工作,你有系统前请确保UI中断监视器添加警报由您的UITest触发!
joern的回答是对的。打印调试描述时看不到任何内容的原因是因为在显示警报之前视图层次结构没有更新。当您下一次尝试与应用程序交互时(忽略警报),视图层次结构将会更新,并且它会发现警报并运行中断处理程序来关闭警报,然后执行您的交互。 – Oletha