Extracting Lazily Loaded Elements for UI Testing in a SwiftUI iOS App

Build robust SwiftUI apps

Photo by Devon Janse van Rensburg on Unsplash

Lazy loading is a powerful optimization technique used by online apps where data is loaded on-demand, and not monolithically. Online apps are constantly sending and receiving data, and any means of cutting down the amount of unnecessarily transacted data is beneficial to both the end-user and the app itself. The app’s backend servers receive less strain, and an end-user isn’t hung up on waiting for data to load. So what’s the problem?

Quick note: if you’d like to skip ahead right to the function & its implementation, skip ahead to “Creating out Custom Swipe Function”

Challenges in Finding Lazily Loaded Elements

When running automated UI tests for iOS, it’s extremely important to note that we are essentially bound to what’s provided by the app’s accessibility hierarchy. An accessibility hierarchy, in terms of iOS and XCUITest, is a textual representation of the app’s user interface. It’s what we’re testing against when using assertion functions to check for the existence of elements in our automated UI tests.

It is effectively our source of truth, and an element set up in-app code can be included or excluded from the hierarchy by having its status as an accessibility object changed. The issue resulting from lazily loaded views and stacks is that an expected element may or may not be loaded. If it has yet to be loaded, it may have the potential to exist, but as far as your UI test is concerned, it does not. We need a way to find what we need.

Example Accessibility Hierarchy: LazyVStack vs. VStack


Extracting Lazily Loaded Elements for UI Testing in a SwiftUI iOS App was originally published in Better Programming on Medium, where people are continuing the conversation by highlighting and responding to this story.

0
(Visited 1 times, 1 visits today)