Skip to main content

Articles, snippets and thoughts about code from Andrew Lord.

Sharing accessibility identifiers between app and tests

Published on · Updated on
1 minute read

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 to me on Twitter.

Thanks for reading and happy coding!

Like what you read? Please share the article.

Avatar of Andrew Lord

Hi, I hope you enjoyed the article. I am Andrew - a builder of apps and developer tools. Articles on the blog focus on all aspects of Android and iOS development using Kotlin and Swift.

Subscribe via RSS