iOS — Accessibility identifier for Appium
UIAutomation
How to add Accessibility identifier for all views in a project in seconds on Python.
One fine sunny morning, I was enjoying my hot coffee and everything was fine, until I heard from our QA Engineer.
We want to run UI Automation on the app. For this, we need you to add Accessibility Identifiers to each view in the iOS app so we can access them.
It would be crazy to add identifiers by hand in such a huge project, with thousands of vies in the Xibs and Storyboards. So, I decided to write a script that will do this for me. I have warm feelings towards Python, so everything decided quickly and the work started. Let’s see how it turned out in the end.
View in XML format
The first question is how to get the views from Storyboard or Xib? For example, I created a Xib file and added UIButton and UILabel. I also added an accessibility identifier from Xcode. If we will open Storyboard or Xib as source code we see regular XML:
This is how our Xib looks in XML format. So, what can we see here? Our main view starts from line 13. Line 16
shows the subviews where we can easily find our button (line 17) and label (line 23). If we look at the button parameters we can find at accessibility with a key “accessibilityConfiguration” and identifier with a value “MyButton” added by hand from Xcode. But adding an identifier by hand is not our purpose, we are lazy and want to automate the process.
Parse XML and add Accessibility identifier
With understanding the hierarchy of the elements inside the XML we know how to get them in the code. All that we need to do, is collects all views without an “accessibility” tag, creates and add this tag to the view. The method that parses an XML by file path looks like this:
The method holds helper property views to store all views that we need to modify. The code runs on all subviews in a root view and adds child views without “accessibility” tag to the collection. After collecting all views we add for each view an Accessibility Identifier. I decided to use the UUID for the identifier value. After we figured out how to parse XML and add identifier, left only one thing, get all Xibs and Storyboards in a project.
Xibs and Storyboards
To start parsing and add an Accessibility Identifier for Xibs or Storyboards we need to find them in the project folder. Let’s see the code:
First, we need to pass a project folder path to the project_path property, otherwise, the code will not work 🙂 We have two helpers properties all_files to store all file paths and ui_files_path to store only Xib and Storyboard paths. In the first loop, we find and save all files in from the project folder. The second loop filters all paths saving only Xib and Storyboard. All that’s left is to iterate over ui_files_path and call add_accessibility_if_needed method for each path. I did it in parallel tasks by using TreadPoolExucuter to process several paths at the same time. All work to our project took a bit less than 2 seconds.
Conclusion
In most cases decision to use UIAutomation on the project comes after the successful launch and the number of users is growing. If your company will decide to use UIAutomation, you will be ready for this. Link to repository https://github.com/OlegTsib/Xcode-Accessibility-Identifier. Good luck!
Just Eat Takeaway.com is hiring! Click here to learn more about our job opportunities