UI Testing Cheat Sheet
This repository is complementary code for my post, UI Testing Cheat Sheet and Examples. The post goes into more detail with example images for most examples.
The included project highlights working code with a simple Test Host. This was last updated for Swift 5 on Xcode 11.4.1.
Basic Functionality
Testing if an element exists
Testing if text with an ellipsis exists
A full text match will find an element even if the displayed text has an ellipsis due to truncation.
Waiting for an element to appear
"Waiting" is now built into XCTest.
Interacting with System Controls
Tapping buttons
Identify buttons by their accessibility label.
Typing text
First make sure the text field has focus by tapping on it.
Dismissing alerts
Dismissing action sheets
Handling system alerts
Present a location services authorization dialog to the user and dismiss it with the following code.
Before presenting the alert add a UI Interruption Handler. When this fires, dismiss with the "Allow" button.
Sliding sliders
This will slide the value of the slider to 70%.
Interacting with pickers
A picker with one wheel:
A picker with multiple wheels. Make sure to set the accessibility delegate so the framework can identify the different wheels.
Tapping links in web views
Interactions
Verifying the current controller's title
Reordering table cells
If you have a UITableViewCell
with default style and set the text to "Title", the reorder control's accessibility label becomes "Reorder Title".
Using this we can drag one reorder control to another, essentially reordering the cells.
Pull to refresh
Create a XCUICoordinate
from the first cell in your table and another one with a dy
of six. Then drag the first coordinate to the second.
Pushing and popping view controllers
Test if a view controller was pushed onto the navigation stack.
Pop a view controller by tapping the back button in the navigation bar and assert that the title in the navigation bar has changed.