Matt Davies Stockton Shares How to Use Deep Links to Speed Up Your UI Tests

Technology

According to Matt Davies Stockton, deep links are links that can redirect users directly to an app instead of an online store or a website. While there are many uses of deep links, you can speed up UI tests drastically with deep links. Let’s figure out how you can do that. 

The Details

1. Project setup

To set up the project, you need to create a new Xcode project named DeepLinkUITest and select Swift as the programming language with SwiftUI as the interface. Check the Include Tests checkbox and start the project. Once you’ve created the project, set up deep links in the Project Navigator and fill in the URlSchemes. 

2. Update the App struct and Content View

Update the DeepLinkUITest file by adding the “@State” variable to choose the texts that need to be shown. You’ll also need to add the “onOpenURL” to handle the deep link and add the “accessibilityModifier” to grab the view in the UITest. Next, you’ll need to wire the text in the ContentView. 

Open the ContentView.swift file and use the Text component to add a variable to the ContentView and update ContentView_Previews. Now you’re ready to run the application and test the deep link. 

3. Open an app with Deep Link

Open Safari and type the deep link. If everything is in order, it should redirect you back to the app. Now you’re ready to create a UI test folder. Create a new Swift file and name it DeepLinkUtils. 

This file should contain tapIfExists and openFromSafari as two static functions with the openFromSafari as the main function that launches the app from Safari.  

4. Initial Test

To check your setup, you can code your first test. Open the DeepLinkUITestUITests.swift file and fill in the testExample code with:

functestExample() throws {

let app = XCUIApplication()

app.launch()

let view = app.staticTexts[“ContentView”]

XCTAssertEqual(view.label, “Hello, World!”)

DeepLinkUtils.openFromSafari(with: “deeplinkuitest://”)

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

XCTAssertEqual(view.label, “From Deeplink!”)

}

This test launches the app, grabs the ContentView, and asserts the initial test as you expect it to. It leverages the utility function to reopen your app from Safari and switches with the current foreground app. 

5. Complex code

You can also run more complex code to navigate to the proper screen and run the test. For instance, you can create a test where you can navigate between home, settings, and product apps and launch a specific product page with a given description. 

With such an approach, you can cut down from 8 to 9 steps to a mere 4 steps where the link launches the app, waits for it to be started, checks that you’re on the right screen, and starts executing the test. For more elaborate apps and conditions, you can save even more time and resources.  

Conclusions

Matt Davies Stockton suggests that you use the above-mentioned steps to use deep links for your UI tests. It saves a lot of time since fewer operations are required. Moreover, UI tests with deep links are end-to-end tests that also check your deep link logic and confirm they are working correctly.