How to use accessibility identifiers in Swift Testing

Accessibility identifiers allow for effective automated UI testing. To use them on any view, add the .accessibilityIdentifier modifier.

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 11/09/2024 at 10:03
Last Updated on 11/09/2024 at 11:21
<p>Three flowers that represent Swift, Swift Testing and XCode side by side. Beneath them sits the text "Accessibility Identifiers".</p>

Accessibility identifiers allow for effective automated UI testing. To use them on any view, add the .accessibilityIdentifier modifier.

SubscribeDownload Open Source Project

The following tutorial shows how we found temporary files from our upcoming app Object Capture Pro. If you wish to be updated on the progress and release of this app, please subscribe using the link below.

Subscribe

Step One: Add an Accessibility Identifier

<p>A screenshot of XCode showing the struct that holds all our accessibility identifiers.</p>

We recommend that you create a struct that holds all the identifiers and then apply them using the .accessibilityIdentifier modifier.

<p>A screenshot showing how we added an accessibility identifier to our MainView.</p>

Step Two: Use the Identifier in a UITest

<p>A screenshot showing code that returns a view that has an accessibility identifier within an app.</p>

In a UITest, use the identifier to find the view.

You can either do by searching the app for any view or to target specific types of views like buttons.

For the code found above, consult the post below.

How to find a view using an accessibility identifier in Swift Testing
<p>A screenshot showing how we search for a specific button within the navigation bar using an accessibility identifier.</p>

For more information consult the documentation below.

Apple Developer: XCUIElementQueryApple Developer: XCUIElementApple Developer: XCUIElement.ElementType

Looking to debug accessibility identifiers?

Consult our guide below to learn how to debug accessibility identifiers.

How to debug accessibility identifiers in XCode

Looking to make nested accessibility identifiers work?

To learn how to use accessibility identifiers within views that use accessibility identifiers, consult the post below.

How to use nested accessibility identifiers in Swift Testing

Looking to learn more about SwiftUI, Swift, Swift Testing, Design and Technology?

Search our blog to learn more about Swift, SwiftUI, Swift Testing, design and technology.

Search our Blog

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
SubscribeContact UsVisit our BlogView our ServicesView our Work

Partner with us

We would love to get to know you and see how we can help your organization with its goals and needs.
Let's Talk

Stay Informed

Get occasional updates about our company, research, and product launches.
Subscribe