This project demonstrates how to share accessibility identifiers, defined as enums, between your app and UI tests in iOS.
The first thing you need to do is create a static library for your acessibility types:
File -> New -> Target -> Static Library -> AccessibilityTypes
Navigate to your project file. You need to link the new library to your app and UI test targets. Do this for both targets:
Now let's use this screen as an example.
Add a new file to the AccessibilityTypes
target and define your accessibilty enum. Make sure it is public
.
public enum WelcomeAccessibility: String {
case loginButton
case createAccountButton
}
From the file inspector, ensure that target membership is applied to the static library.
In your view controller file, define a fileprivate
extension to UIAccessibilityIdentification
that implements a computed property wrapping the accessibilityIdentifier
property.
fileprivate extension UIAccessibilityIdentification {
var testID: WelcomeAccessibility? {
get { fatalError() }
set { accessibilityIdentifier = newValue?.rawValue }
}
}
And don't forget to import the static library:
import AccessibilityTypes
Also in your view controller file, define a method that configures accessibility your all of your view components. Then call the method from viewDidLoad()
.
private func configureAccessibility() {
loginButton.testID = .loginButton
createAccountButton.testID = .createAccountButton
}
Now let's shift our attention to the test case. But first, let's implement a handy extension that allows us to use the accessibility enum as a subscript key.
extension XCUIElementQuery {
subscript(key: WelcomeAccessibility) -> XCUIElement {
self[key.rawValue]
}
}
And finally, let's write the test case!
func testLoginFlow() {
let app = XCUIApplication()
app.launch()
app.buttons[WelcomeAccessibility.loginButton].tap()
// ... Add more test code ...
}
Enjoy!