Skip to main content

Sharing accessibility identifiers between app and tests

Updated:

For identifying views within UI tests we can use accessibility identifiers, which are String values. We can register all of our identifiers within an enum, using an extension to set them on our views. By adding the enum to the app target and the UI test target, an extension can be added to find views within tests suing our ID enum. It's really nice to use an enum, avoiding duplicating the strings and avoiding errors!

App sources

extension UIAccessibilityIdentification {
    var viewAccessibilityIdentifier: ViewAccessibilityIdentifier? {
        get { fatalError("Not implemented") }
        set {
            accessibilityIdentifier = newValue?.rawValue
        }
    }
}

addContactButton.viewAccessibilityIdentifier = .addContactButton

Test sources

extension XCUIElementQuery {
    subscript(key: ViewAccessibilityIdentifier) -> XCUIElement {
        self[key.rawValue]
    }
}

XCUIApplication().buttons[.addContactButton].tap()

I hope the article was useful. If you have any feedback or questions please feel free to reach out.

Thanks for reading!

Like what you read? Please share the article.

Avatar of Andrew Lord

WRITTEN BY

Andrew Lord

A software developer and tech leader from the UK. Writing articles that focus on all aspects of Android and iOS development using Kotlin and Swift.

Want to read more?

Here are some other articles you may enjoy.