【问题标题】:How to open url in Safari and the get back to the app under UITests in Xcode 7?如何在 Safari 中打开 url 并在 Xcode 7 中的 UITests 下返回应用程序?
【发布时间】:2015-12-07 22:56:19
【问题描述】:

这是我的自定义视图,其中"LondonStreet" 是一个按钮。

当我点击该按钮时,我会得到 url 并在 Safari 中打开它(它可以工作)。然后我可以返回,使用"Back to Wishlist" 按钮(它也可以)。

问题是当我尝试在 UITests 下测试它时。

itemsTable.cells.elementBoundByIndex(0).buttons["addressButton"].tap() //press the button to open link in Safari

连同这一行:

app.statusBars.buttons["Back to Wishlist"].tap() //go back, doesn't work, although it was recorded by Xcode itself.

是一个错误:

UI 测试失败 - 未能在 5 秒内截屏。

还有问题导航器

UI 测试失败 - 无法及时更新应用程序状态。

【问题讨论】:

    标签: swift ios9 xcode-ui-testing xcode7.1beta


    【解决方案1】:

    从 iOS 11 开始,您可以使用 XCUIApplication(bundleIdentifier:) 初始化程序与其他应用程序交互。

    要返回您的应用,您需要执行以下操作:

    let myApp = XCUIApplication(bundleIdentifier: "my.app.bundle.id")
    let safari = XCUIApplication(bundleIdentifier: "com.apple.mobilesafari")
    
    // Perform action in your app that opens Safari
    
    safari.wait(for: .runningForeground, timeout: 30)
    myApp.activate() // <--- Go back to your app
    

    【讨论】:

    • 答案很棒;)
    • 对于 Xcode 13 和 iOS 15,这似乎会间歇性地失败 Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid pid -1'
    • @bencallis 也有同样的问题
    • 这里有同样的问题。 iOS 15 模拟器上的 Xcode 13 - 出于某种原因,测试运行器有时会在等待方法上崩溃。
    • @bencallis 尝试在每次测试开始时终止 Safari 应用程序。解决了我们测试中的“无效 pid -1”崩溃问题。
    【解决方案2】:

    UI 测试不能与应用程序之外的任何东西进行交互。在您的场景中,一旦您的应用打开 Safari,框架就无法再做任何事情。

    要验证这一点,请尝试在 Safari 打开后打印出应用的层次结构。您会注意到 Safari 中的任何内容和导航栏都不会显示 - 您只会看到应用程序的信息。

    print(XCUIApplication().debugDescription)
    

    【讨论】:

    • 这条(我的)评论不再适用于 iOS 11+ :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-25
    • 2016-11-30
    • 2018-04-15
    相关资源
    最近更新 更多