Setting Accessibility Identifiers for Appium in iOS
Using Accessibility Identifiers makes Appium tests less brittle
by Lou Franco
If you are considering Appium or any other GUI testing framework for iOS, like XCUI Tests, then the first thing you should do is start using accessibility identifiers.
Complex queries make tests brittle
The main job of a GUI test script is to control the app by interacting with the app's view components. The way you do that is by writing queries to identify the views in the view hierarchy. For example, you could write a script that taps the 2nd button in the navigation bar.
But queries like that are extremely brittle and will fail when you add a button that changes the index of the button you want to tap.
You can also identify buttons in the query by using the button's text (or even its accessibility label), but those could also change in the future and will definitely be different if you want to GUI test other localizations.
The one thing that will be relatively stable is the view's accessibility identifier. There is no reason that it ever needs to change, and you should not set it to a localized string since it is never shown (or spoken) to the user.
You can set it to any string, but it does need to be unique in the view hiearchy, so it makes sense to prefix it with something identifying the screen that it is on.
Setting accessibility identifiers in UIKit
In UIKit, every
UIView has an
accessibilityIdentifier property. You can set it like this:
shareButton.accessibilityIdentifier = "HomeVC_shareButton"
Setting accessibility identifiers in SwiftUI
In SwiftUI, every
View has an
accessibility(identifier:) modifier. You can set it like this:
Text("App Title") .accessibility(identifier: "HomeView_title")
Setting Accessibility Identifiers in a Storyboard or XIB
If you use Storyboards or XIB files to make your views, then you'll find the accessibility identifier in the Identity Inspector in the right sidebar (CMD-OPT-4)
You can use any string, but you should establish some style guide for how strings are generated. This will make it easier for test script writers to keep their code maintainable.
Other uses of an accessibility identifier
Once you have them, there are other ways they could be used.
- In logging
- In analytics
- Instead of tags (in UIKit) for finding views programmatically
Next Article: Make a Test Build to use with Appium