Sharing accessibility identifiers between app and tests
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!
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
Want to read more?
Here are some other articles you may enjoy.
Create Xcode file templates and share them with your team
When creating new files in Xcode the built-in templates often contain code we immediately delete or need to alter. We will explore the process of creating our own templates and how to go about sharing them with the rest of our team members.