You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 06:10:53