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

iOS Swift下特定号码通话功能的UI测试问题求助

解决iOS拨打电话功能的UI测试问题

问题根源

你当前的测试代码存在两个核心问题:

  • 系统弹出的呼叫确认框属于SpringBoard(系统应用),而非你的目标App,直接用app无法定位到这些元素
  • 元素定位方式错误,且使用sleep这种不可靠的等待方式

修正后的UI测试代码

import XCTest

class PhoneCallUITests: XCTestCase {
    var app: XCUIApplication!
    var springboard: XCUIApplication!

    override func setUpWithError() throws {
        continueAfterFailure = false
        app = XCUIApplication()
        app.launch()
        // 获取系统SpringBoard应用实例
        springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")
    }

    func testPhoneCallFlow() {
        // 点击App内的呼叫按钮
        let callButton = app.buttons["Call"]
        XCTAssertTrue(callButton.waitForExistence(timeout: 5))
        callButton.tap()

        // 等待系统呼叫确认弹窗出现,定位"取消"按钮并点击
        let cancelButton = springboard.buttons["取消"]
        XCTAssertTrue(cancelButton.waitForExistence(timeout: 5))
        cancelButton.tap()

        // 再次点击App内的呼叫按钮
        XCTAssertTrue(callButton.waitForExistence(timeout: 5))
        callButton.tap()

        // 等待系统弹窗,定位"呼叫"按钮并点击
        let systemCallButton = springboard.buttons["呼叫"]
        XCTAssertTrue(systemCallButton.waitForExistence(timeout: 5))
        systemCallButton.tap()
    }
}

关键修改说明

  • SpringBoard访问:系统级弹窗(如呼叫确认框)归属于com.apple.springboard,必须通过这个bundleID获取对应的XCUIApplication实例才能定位元素
  • 替换sleep为waitForExistence:waitForExistence会等待元素出现后再执行操作,比固定时长的sleep更稳定,适配不同设备的响应速度
  • 修正元素定位:系统弹窗的按钮文本是中文的"取消"和"呼叫"(根据系统语言调整),且属于普通buttons类型,而非alerts或popUpButtons

额外注意事项

  • 确保测试设备的系统语言与代码中按钮文本一致,如果是英文系统,按钮文本应为"Cancel"和"Call"
  • 在测试目标的Info.plist中添加telprompt到LSApplicationQueriesSchemes数组,确保App可以正常打开呼叫链接

内容的提问来源于stack exchange,提问作者Datta Bhaskar

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 17:52:18