VisionOS中如何通过UITest访问系统弹窗并模拟按钮点击?
在VisionOS的UITest中访问并点击系统弹窗按钮的方法
VisionOS 中不再通过 Springboard 访问系统弹窗,这类弹窗直接挂载在测试应用的 UI 层级下,你可以直接从测试应用的 XCUIApplication 实例中定位并操作按钮:
let app = XCUIApplication() // 可选:等待弹窗加载,提升测试稳定性 if app.alerts.firstMatch.waitForExistence(timeout: 5) { // 定位并点击"取消"按钮 let cancelButton = app.alerts.buttons["取消"].firstMatch if cancelButton.exists { cancelButton.tap() } }
额外说明:
- 不管是权限请求弹窗还是系统对话框,VisionOS 都会将其归属到当前测试应用的 UI 树中,无需再调用 Springboard
- 如果弹窗有明确标题,也可以通过标题精准定位:
let specificAlert = app.alerts["权限请求"].firstMatch if specificAlert.exists { specificAlert.buttons["允许"].tap() } - 使用
waitForExistence是个好习惯,能避免因弹窗加载延迟导致的测试失败问题
内容的提问来源于stack exchange,提问作者General Failure
相关产品推荐
相关产品推荐

