Open universal links (deep links) with XCUITest

Viewed 2811

I would like to create several tests for native iOS application. To be more precise, I want to test deep links. But I am not sure how to trigger deep link with XCUITest and I don't really see how launch() and launcArguments (https://developer.apple.com/documentation/xctest/xcuiapplication) can help me. Did anybody have a chance to open deep link with XCUITest?

3 Answers

I never tried this, but this idea comes to my mind. Create a new dummy project/application that should only contain some links pointing to URLs that you expect your original app to open. From that new application, write some UI tests that tap a link, like this:

func testOpeningLinks() {
    let app = XCUIApplication()
    app.links["Some link text"].tap()
    // This is the place where your original app should be opened...

    // Find the XCUIApplication object:
    let originalApp = XCUIApplication(bundleIdentifier: "original.app.bundle.identifier")
    // You should be able to find some views from original app from here, eg. a button:
    let button = originalApp.buttons.element
}

This would work only if you have previously installed your app on the device/simulator where you're running your UI tests.

  1. Set safari as app like this

    let safari = XCUIApplication(bundleIdentifier: "com.apple.mobilesafari")

  2. Open your email in safari

  3. Tap on the link
  4. Normally assert some element on app

In iOS 14 using Spotlight seems to work well:

    private func open(_ urlString: String) {
        XCUIDevice.shared.press(.home)
        XCUIApplication(bundleIdentifier: "com.apple.springboard").swipeDown()
        let spotlight = XCUIApplication(bundleIdentifier: "com.apple.Spotlight")
        spotlight.textFields["SpotlightSearchField"].typeText(urlString)
        spotlight.buttons["go"].tap()
    }
Related