如何在 Safari 中打开 url 并在 Xcode 7 中返回到 UITests 下的应用程序?

How to open url in Safari and the get back to the app under UITests in Xcode 7?

这是我的自定义视图,其中 "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 Testing Failure - Failed to get screenshot within 5s.

并且也在 issue Navigator

UI Testing failure - Unable to update application state promptly.

UI 测试无法与应用程序之外的任何内容交互。在您的场景中,一旦您的应用程序打开 Safari,框架就无法再执行任何操作。

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

print(XCUIApplication().debugDescription)

从 iOS11 开始,您可以使用 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

要在 iOS 15 上在 Safari 中打开特定 url:

safari.textFields["Address"].tap()
safari.textFields["Address"].typeText("www.urlToOpen.com")
safari.keyboards.buttons["Go"].tap()