iOS deep linking with Xcode UI testing

I want to test parts of my app UI in isolation, so avoid having to drill down from the splash screen to the pages in question. My app implements deep linking, but I can't figure out how to configure XCUIApplication in the setUp() to trigger the page transition.


I have a method in the app delegate called openURL() which handles this for the production app. It works great, but when I call it (below), nothing happens. If I put a breakpoint in openURL() it never even gets hit.


    override func setUp() {
        super.setUp()
        continueAfterFailure = false

        XCUIApplication().launch()
      
        guard let url = NSURL(string: "fooapp://page/3") else {
            fatalError("Couldn't create URL")
        }


        UIApplication.sharedApplication().openURL(url)
    }


What I really want is to set UIApplicationLaunchOptionsURLKey in launchOptions and just let the app behave as if it received an external URL. I see launchArguments and launchEnvironment are available on XCUIApplication, but then I have to write something special to extract those values out and manually call openURL somewhere in the app delegate. What's the right way to do this?

Replies

We agree that it would be great if this could work. It is a "known issue" that it doesn't. Please file a bug report to request making this possible, so that we can better gauge how many developers that are interested in this request.

The scenario of applying URL is frequently used in our test environment. We try not to "hijeck" code of our app under test. The only workaround we found is to call UIApplication.sharedApplication().openURL() before XCUIApplication().launch() in Setup() method. However it seems only working for the first time.

Could we add applying url method in UI Testing API? Thank you so much!

Hi Joar

Just happen to see this thread, our company also have lots of deep links implemented in our iso app, and could love this deep link stuff work, I just tried pretty muc the same thing @timo_HZ mentioned, and got the same result, seems till now the deep link in uitesting still not work.


Leo

Hello, any progress? 😟

Any progress on this

Almost 5.1k developers that are interested in this request. Any update so far?

I use this:

func open(appPath pathString: String) {

        openFromSafari("com.myapp.myapp://my.domain.com\(pathString)")

        XCTAssert(app.wait(for: .runningForeground, timeout: 5))

    }



    private func openFromSafari(_ urlString: String) {

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

        safari.launch()

        // Make sure Safari is really running before asserting

        XCTAssert(safari.wait(for: .runningForeground, timeout: 5))

        // Type the deeplink and execute it

        let firstLaunchContinueButton = safari.buttons["Continue"]

        if firstLaunchContinueButton.exists {

            firstLaunchContinueButton.tap()

        }

        safari.textFields["Address"].tap()

        let keyboardTutorialButton = safari.buttons["Continue"]

        if keyboardTutorialButton.exists {

            keyboardTutorialButton.tap()

        }

        safari.typeText(urlString)

        safari.buttons["go"].tap()

        let openButton = safari.buttons["Open"]

        let _ = openButton.waitForExistence(timeout: 2)

        if openButton.exists {

            openButton.tap()

        }
    }