ajc6432
ajc6432

Reputation: 188

Xcode UI testing for external launching

Hope the title isn't too vague. In the app I am testing, certain app-flow launches external apps (like Safari or Facebook for example). How can I verify that the app launched them with a UI test? I can test for like an openURL with a unit test but is there an equivalent for UI?

I'm not trying to actually continue after leaving the app, just to test and ensure the appropriate new app or URL was launched. The simulator/recorder can select UI elements from the launched application, but the test breaks at that point of the code. I also tried getting a handle to something on the menu bar (always present in the app, like a hamburger button) while it was there and then checking for it after launching the other app (to make sure it wasn't there). But that broke the test as well.

Is there a work around? Or is this just something to be tested by a Unit Test?

Upvotes: 3

Views: 2088

Answers (2)

caldera.sac
caldera.sac

Reputation: 5088

Xcode 13 and iOS 15 for safari : we could check if safari opens as below.

let safari = XCUIApplication(bundleIdentifier: "com.apple.mobilesafari")
let isSafariBrowserOpen = safari.wait(for: .runningForeground, timeout: 30)

Then do what you want :

if isSafariBrowserOpen {
    // Safari browser has opened, then additionally we could check if the url isn't nil
    safari.textFields["Address"].tap()
    let url = safari.textFields["Address"].value as? String
    XCTAssertNotNil(url)
}
else {
    // Safari browser hasn't opened
    // do something here, if necessary
}

Note: I didn't check with other external apps.

Upvotes: 0

cakes88
cakes88

Reputation: 1907

As you mentioned, the UI framework can only test the given application. So I would do an assert to make sure that the screen you were previously on (before opening safari or facebook, etc) is no longer present. So for example:

XCTAssertFalse(app.tables.elementBoundByIndex(0).exists, "Found element, so app didn't open safari/facebook")

You're just asserting that the element isn't there, change app.tables.elementBoundByIndex(0).exists to be whatever element you're checking.

Upvotes: 1

Related Questions