Search code examples
swiftios9xcode-ui-testingxcode7.1beta

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


This is my custom view where "LondonStreet" is a button.

enter image description here

When I tap that button I get url and open it in Safari (it works). Then I can go back, using "Back to Wishlist" button (it also works).

enter image description here

The problem is when I try to test this under UITests.

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

Along with this line:

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

is an error:

UI Testing Failure - Failed to get screenshot within 5s.

And also in issue Navigator

UI Testing failure - Unable to update application state promptly.

enter image description here


Solution

  • 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