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()

Thanks for reading and happy coding! πŸ™