UI 测试:从 UITextView 打开 URL 方案后找不到跳板按钮元素

发布于 2025-01-09 20:15:19 字数 3314 浏览 1 评论 0原文

在 UI 测试中,我尝试点击 call 按钮和 cancel 按钮,如下图所示。当用户单击 UITextView 中的电话号码链接时,会触发该按钮。

这个线程说我们应该使用跳板来处理URL方案弹出窗口。但是当我 po 东西时,我在控制台中收到错误。

这是lldb中的错误。

-[XCTFuture _waitForFulfillment] 中断言失败,XCTFuture.m:159 错误:执行被中断,原因:ObjC 内部异常断点(-7)..

并且我创建了以下最小应用程序来重现此问题。

输入图片此处描述

在此处输入图像描述

ViewController

import UIKit

class ViewController: UIViewController, UITextViewDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = "Main View"
        
        addUITextViews()
    }
    
    func addUITextViews(){
        
        // lauout for the View
        let myTextView3 = UITextView()
        myTextView3.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(myTextView3)
        
        let g = view.safeAreaLayoutGuide
        NSLayoutConstraint.activate([
            myTextView3.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 40),
            myTextView3.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: 40),
            myTextView3.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            myTextView3.centerYAnchor.constraint(equalTo: view.centerYAnchor),
            myTextView3.widthAnchor.constraint(equalToConstant: 400),
            myTextView3.heightAnchor.constraint(equalToConstant: 400),
        ])
        
        // set Phone# and Email Address for the UITextView
        myTextView3.font = UIFont.systemFont(ofSize: 24)
        myTextView3.isEditable = false
        myTextView3.isSelectable = true
        myTextView3.text = "Phone: 888-111-2222 \nEmail: [email protected]"
        myTextView3.dataDetectorTypes = .all
        myTextView3.delegate = self
    }
    
    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {

        guard let currentScheme = URL.scheme else { return false }

        switch currentScheme {
        case "sms", "tel":
            print("It is a call!")
        case "mailto":
            print("send email")
        default: break
        }
        return true
    }
}

UI 测试

import XCTest

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

    override func setUpWithError() throws {
        app = XCUIApplication()
        springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")
        app.launch()
        
        continueAfterFailure = false
    }
    
    func testPhoneCall() {
        app.links["888-111-2222"].tap()
        sleep(1)
        
        springboard.buttons["Call (888) 111 2222"].tap()
        // or try
        springboard.buttons["Cancel"].tap()
    }
}

In UI test, I try to tap the call button and cancel button as below screenshot showed. That buttons are triggered when a user click on the phone number link from a UITextView.

And this Thread said we should use springboard to deal with URL scheme popups. But I get errors in console when I po the things.

That is the error in lldb.

Assertion failure in -[XCTFuture _waitForFulfillment], XCTFuture.m:159
error: Execution was interrupted, reason: internal ObjC exception breakpoint(-7)..

And I have created below minimum app to reproduce this problem.

enter image description here

enter image description here

ViewController

import UIKit

class ViewController: UIViewController, UITextViewDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = "Main View"
        
        addUITextViews()
    }
    
    func addUITextViews(){
        
        // lauout for the View
        let myTextView3 = UITextView()
        myTextView3.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(myTextView3)
        
        let g = view.safeAreaLayoutGuide
        NSLayoutConstraint.activate([
            myTextView3.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 40),
            myTextView3.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: 40),
            myTextView3.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            myTextView3.centerYAnchor.constraint(equalTo: view.centerYAnchor),
            myTextView3.widthAnchor.constraint(equalToConstant: 400),
            myTextView3.heightAnchor.constraint(equalToConstant: 400),
        ])
        
        // set Phone# and Email Address for the UITextView
        myTextView3.font = UIFont.systemFont(ofSize: 24)
        myTextView3.isEditable = false
        myTextView3.isSelectable = true
        myTextView3.text = "Phone: 888-111-2222 \nEmail: [email protected]"
        myTextView3.dataDetectorTypes = .all
        myTextView3.delegate = self
    }
    
    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {

        guard let currentScheme = URL.scheme else { return false }

        switch currentScheme {
        case "sms", "tel":
            print("It is a call!")
        case "mailto":
            print("send email")
        default: break
        }
        return true
    }
}

UI Test

import XCTest

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

    override func setUpWithError() throws {
        app = XCUIApplication()
        springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")
        app.launch()
        
        continueAfterFailure = false
    }
    
    func testPhoneCall() {
        app.links["888-111-2222"].tap()
        sleep(1)
        
        springboard.buttons["Call (888) 111 2222"].tap()
        // or try
        springboard.buttons["Cancel"].tap()
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文