My notes on UI-testing in Xcode
- Add UITesting to your XCode project when you setup the XCode project
- Set your cursor inside a test method and click the red record button in the right corner of xcode,
- click around the app to generate programatic UI-method calls
- Create a check:
XCTAssertEqual(app.tables.cells.count, 56, "There should be 56 words matching 'test'")etc
- Use the Accessibility inspector to look at what your UI elements’ accessibility values are before you even need to run the test. Use this to make sure that the accessibilityLabel you assigned for your UI element is being found, or if the element is being considered an accessibility element at all. However, you’ll note that you can’t see the accessibilityIdentifier on this view. Nonetheless it is very useful especially to determine if your element is visible to accessibility in the first place. If you find you can’t highlight the view you’re looking for, chances are the superview is intercepting the accessibility focus, and you need to set its isAccessibilityElement to false.
- Internal methods will run automatically in the test. Make them private to run them from setup.
- Prefer using:
waitForExistence(timeout:)over a regular
- Prefer using
- Open the
Accessibility Inspector.appin macOS as a way of identifying accessibility ids.
- for iOS there is the:
iOS Simulator's Accessibility Inspector
- To access elements by accessibility ids: set/override:
accessibilityLabelwith an id and set/override the
- For some strange reason sometimes only
accessibilityIdentifierworks and you have to set the
isAccessibilityElementsetting or overide works
- Find accessibility elements by:
element.label == "someLabel"if you are using accessibilityIdentifier or
element.identifier == "someId"if you are using accessibilityLabel
XCUIElementQuery.debugDescriptionto debug a query (Accessibility Hierarchy)
- ⚠️️ IMPORTANT ⚠️️ Containers can have accessibilityIdentifier but they should have accessibility turned off. Logic is that we don’t interact with containers, but we do need to access testing via accessibility hierarchy. setting isAccessibilityElement to true on a container will cause problems with UITesting. Setting it on leaf elements such as buttons etc is fine.
- Parsing a queries is much faster than parsing element
- ️⚠️ IMPORTANT ⚠️️ Don´t try to store references to elements outside the scope where you use it, as you interact with the UI hierarchy, it will change, and it must be traversed again
- Keep animations enabled. As things can be different if you don’t have animations. A suggestion is to set the view speed to 2x or 4x
- XCUIApplication: This class responsible for launching, terminating apps. ⚠️️ Not a singleton. ⚠️️
- XCUIElement: This class used to determine the UI element in the app and able to perform any action on it e.g tap, double tap, swipe etc
- XCUIElementQuery: This class can be used to uniquely identify the UI element on the screen.
We can form the query to uniquely identify the UI element on the screen. Imagine that you have to select the first button on the app. We can form the query like this XCUIApplication().
app.alerts.element app.buttons.element app.collectionViews.element app.images.element app.maps.element app.navigationBars.element app.pickers.element app.progressIndicators.element app.scrollViews.element app.segmentedControls.element app.staticTexts.element app.switches.element app.tabBars.element app.tables.element app.textFields.element app.textViews.element app.webViews.element
- Great way to make Unit-tests that matters.
- Careful to not go overboard by testing everything, as that will make the app harder to develop on later
- A basic run-through of UI-testing: https://medium.com/exploring-ios/an-introduction-to-ui-testing-on-ios-697526fa0fa9
- Commonly used UI-Test method calls: https://www.hackingwithswift.com/articles/148/xcode-ui-testing-cheat-sheet
- Properties of XCUIElement: https://metova.com/a-guide-to-xcode-ui-test/
- Cheat sheet on github: https://github.com/joemasilotti/UI-Testing-Cheat-Sheet
- TestExtension with lots of reusable code: (also testing languages) https://github.com/PGSSoft/AutoMate
- video tut: https://www.raywenderlich.com/3619-207-xcode-ui-testing
- video 2: https://www.raywenderlich.com/3573-beginning-ios-unit-and-ui-testing/lessons/5
- Working with webview and facebook: https://hackernoon.com/running-uitests-with-facebook-login-in-ios-4ac998940c42