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

荒凉一梦 提交于 2019-12-03 05:47:30

Starting in iOS 11 you can interact with other applications using the XCUIApplication(bundleIdentifier:) initializer.

To get back to your app you'd do something like:

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

UI Testing cannot interact with anything outside of your application. In your scenario, the framework can no longer do anything once your app opens Safari.

To verify this, try printing out the app's hierarchy once Safari opens. You will notice that nothing in Safari nor the navigation bar will show up - you will only see your app's information.

print(XCUIApplication().debugDescription)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!