swift - XCUITest 验证 ui 中断处理程序发生
问题描述
我对 swift 和 xcuitest 很陌生。我最近遇到addUIInterruptionMonitor
了处理可以弹出的警报。我想知道的是如何验证警报是否发生以及处理程序是否已处理。以以下为例
addUIInterruptionMonitorWithDescription("Location Services") {
(alert) -> Bool in
alert.buttons["Allow"].tap()
return true
}
app.buttons["Request Location"].tap()
app.tap() // need to interact with the app again for the handler to fire
// after this if the handler never gets called I want the test to fail
我想测试警报是否真的发生,但据我了解,在我最后一次之后,tap()
如果警报从未被触发,我的处理程序将不会被调用。我需要测试警报是否真的发生了,然后可能会在处理程序的内容中添加一些断言
解决方案
我似乎已经回答了我自己关于进一步调查的问题。对于使用 xcuitest 进行的异步测试,我可以使用XCTestExpectation,它在创建时会导致测试等到期望得到满足,或者在某个超时后失败。这样我上面的代码就变成了:
let expectation = XCTestExpectation(description: "Location Service Alert")
let locationMonitorToken = addUIInterruptionMonitorWithDescription("Location Services") {
(alert) -> Bool in
alert.buttons["Allow"].tap()
expectation.fulfill() // test waits for this before passing
return true
}
app.buttons["Request Location"].tap()
app.tap() // alert triggered here
wait(for: [expectation], timeout: 3.0)
removeUIInterruptionMonitor(locationMonitorToken)
更新:忘记放入wait(for: [expectation], timeout: 3.0)
触发后的警报以确保调用处理程序。
推荐阅读
- openthread - 替换 Wpantund 已弃用
- python - 缺少可选依赖项“gcsfs”。处理 GCS 文件需要 gcsfs 库使用 pip 或 conda 安装 gcsfs
- kubernetes - “--dry-run”的无效参数“client”
- r - 如何使用 dplyr 函数而不报错?
- wordpress - 在配置 nginx 以在我的子域上打开 wordpress 时遇到问题
- python - 我安装 Geopandas 出了什么问题?
- android - 如何使 DefaultTimeBar 始终显示在 exoplayer
- vb.net - 请求列表框第一项的第一个和第三个字符返回第二个和第四个
- xamarin.forms - 在 CollectionView 中选择项目时如何播放动画?
- time-series - 谷歌地球引擎:从 S2 NDVI 图像集合中删除值(5 和 95 个百分位)