Making your iOS App accessible for UI Tests (Aya’s cookbook about iOS Accessibility Identifiers)

Aya Akl
5 min readApr 19, 2020

P.S: using XCUITest and Xcode

To start things right, you should consider doing the correct things first which will lead you to the stability and maintainability of the things you are working on. For the UIAutomation of iOS tests, you should consider making your iOS app accessible to tests and therefore the only way to do this is to care for the Accessibility Identifiers which is the first correct thing to do in your journey of writing UI tests for iOS Apps.

So this post will be on some questions and the answers for them so that to illustrate more on what is this accessibility identifier, how to deal and work with them.

Also this post will be talking on how to apply them in tests with XCUITest.

Let’s begin of what is an accessibility identifier? Apple has introduced accessibility identifiers to be used by UIAutomation which is a unique ID to identify a UI element in an iOS app for usage of the UI Automation.

Why is this accessibility identifier important? one of the drawbacks of automating UI tests (as people complaining) that they are unstable and flaky, so we need to ensure that this is not the case in our iOS UI tests and making sure to use accessibility identifiers ensure this.

Because it gives us a unique ID to identify our elements, this will ensure that we are not searching for multiple elements nor making the test fail because of depending on some UI aspects, like coordinates or xpath.

Imagine you are doing a refactor to some UI in your app and you decided to change a place of a UIButton in an app screen, when you are depending on just some coordinates or labels, the UI tests will easily break and be flaky but depending on unique accessibility identifier, the test shall remain stable because the UIButton location only changed but the UIButton is still there and can be located.

So in order to prevent your iOS UI tests to be flaky, accessibility identifier should be your first step to go.

How do we do the selection of element based on an accessibility identifier? The most basic way to achieve this is to search for this element by using the id you added to your UIElements that in the iOS app source code. So in your test

P.S: Bear in mind that you can do this as generic as possible when you have generic helper method finding this elements (XCUIElements) by Accessibility Identifier.

Now to the part I call FUN 😏😒🤷🏻‍♀️, how can we apply those accessibility identifiers inside the app source code so we can be able to use it in our Tests and Test Target?

I can remember that the first time I had to work on this years back ago I was like WHAT CAN I DO NOW 🙄🙄 but now I can proudly say that I think I know how to add accessibility identifiers by all means and kinds iOS Apps (Swift and Objective-C), dynamic, static, different implementation inheritance for interface elements, different flows and everything!

P.S: You can call me the iOS Accessibility Identifiers Expert 😉

So back to the FUN, I will start from the easy ways coming forward to the so-called-not-so-straight-forward-ways.

1- Add .accessibilityIdentifier to the interface element of your iOS App

Given that you have a loginButton, so you need to just assign a string value to its accessibility identifiers as follows:

Most probably in this case, your app screen has many interface elements in the view and inside viewDidLoad(), you just pass this accessibility identifier. Of course it is better to use a method for adding all the accessibility identifiers for the interface elements you have in that view or if you have a reusable method for setting all accessibility stuff 😉

2- Adding these accessibility identifiers to a computed property

Given a view in the app code is focused on one UI element which is the addNoteButton.

So maybe it is better to add this button in a computed property and setting the accessibility identifier there.

3- Now, it will get a little bit tricky and *Dynamic*

Given that you want to add a dynamic accessibility identifiers which means this accessibility identifier is added based on some data coming from your backend server

P.S: I will illustrate this backend+UI cases for accessibility identifiers a little bit more in future posts

So, you should look on where these calling of the APIs that gets your data from the backend server, and then add them as an accessibility identifier as follows:

4- Adding accessibility identifiers to a cell in a UICollectionView

Given that you have a collection view with cells, each cell should have its own properties. Instead of adding the accessibility identifiers for the cells inside the place the collection view is stored, it is better to the accessibility identifier to each cell as their properties.

Another example of cells, if we have a table of cells and there are multiple of labels under a cell, we also can easily work with this and give the accessibility identifiers when configuring the cell as follows:

5- Adding accessibility identifiers for a different implementation of an existing interface element

Using different kind of implementation or customisation to a UITextField, a UIButton, etc…

6- Adding accessibility identifiers for same interface element that will be used in different views

So to the last part that will connect the dots, how can we connect both the app source code with the test target to use these accessibility identifiers in a reusable and maintainable way?

For this we will be using Enums.

But first you should know divide your app into set of screens and you call the corresponding accessibility identifiers for those set of screens which will make reusing these accessibility identifier is an easy, functional, and meaningful way.

We need a main Enum which will hold all of those set of accessibility identifier per screen:

enum AccessibilityIdentifier{...}

Then we create a set of enums under this main enum per our screens:

And last but not least, we add our accessibility identifiers and store them in each enum of each screen:

P.S: Not finished yet

In order to connect those accessibility identifiers in both test and app target, you need to relate them to schemes and Voila 😉

So the final step is to use the accessibility identifier enum in your both app and test code as follows:

With more experience in this matter, I will be adding more ways of adding these accessibility identifiers into my cookbook.

Posted on blogging, ios, Mobile App Testing, test automation, testing, XCUITest

Originally published at http://ayaakl.wordpress.com on April 19, 2020.

HAPPY TESTING!

--

--

Aya Akl

#Quality is my thing - Passionate human being who wants to change the world!! #MobileTester #SDET #iOS #Testing