What is UI testing?
In simple terms. UI testing is interacting with an app’s UI elements by tapping, swiping, scrolling and verifying behaviour.
Automating UI tests can save you plenty of time, especially during regression testing, but at the same time automation testing needs to simulate the human’s behaviour which is not always straightforward.
Thankfully, Xcode provides you with the XCUIElement class which allows gestural interactions such as:
func tap() func doubleTap() func twoFingerTap() func tap(withNumberOfTaps: UInt, numberOfTouches: UInt) func press(forDuration: TimeInterval) func press(forDuration: TimeInterval, thenDragTo: XCUIElement) func swipeLeft() func swipeRight() func swipeUp() func swipeDown() func pinch(withScale: CGFloat, velocity: CGFloat) func rotate(CGFloat, withVelocity: CGFloat)
Xcode setup for UI testing
Create UI testing target.
If you have an existing project and would like to add automated UI tests to it, first you need to create iOS UI testing target. This is how you do it.
- Open your Xcode project.
- Go to: File -> New -> Target
- From the window Choose a template for your new target: select iOS UI Testing Bundle and hit Next:
- From the window Choose options for your new target: select your Team and Target to be tested
- Select Finish button and new test target has been created.
Create UI test file.
1. Pick the location in your Project navigator where would you like your test file to be created.
2. Right-click and select New File…
3. From the window Choose a template for your new file select UI Test Case Class and hit Next button.
4.In the Choose options for your new file: window provide class name and hit Next button.
5. Select the location where you want the file to be created and hit Create button.
6. You have just created your UI test class with
Remember: Your test methods always have to start with the “test” word, otherwise you won’t be able to run them.
Once you are set up with the target and have created your test method you can start using the handy UI recorder which will generate the test script and identify the UI elements for you.
Recorder works with both simulators and physical devices, although you might sometimes find it easier to identify the UI element to use one or the other.
To kick off recording the tests go to your test method and hit the red dot button placed next to the Debug area.
Once you press the button, your application should be launched on simulator or device depending which one you have selected in Xcode. From now on, every interaction with the application will be recorded and the test script will be generated in your test method including your UI elements’ identifiers.
Here’s an example of a test where user taps on the email field and types their username to login to the app:
let emailAddressTextField = application.collectionViews.scrollViews.otherElements.textFields["Email address"] emailAddressTextField.tap() emailAddressTextField.typeText("email@example.com")
While recording your tests and interacting with the app I experienced the Xcode bug saying “Timestamped Event Manager Error: Failed to find matching element”.
That error means that the Xcode has not found the UI element that we interacted with. At that point the recording stopped automatically. For me the solution was to simply start recording again and interact with the element.
Run your tests
There are couple of ways to run your tests.
1.In Xcode go to Product -> Test (cmd+u). This will run all the tests in the project including unit tests.
2.If you want to run only the UI tests go to Test navigator. If your UI tests are located in one folder you can run all of them by pressing the play icon next to the folder where your tests are located.
3.There might be a situation in which you won’t want to run all your tests. If you would like to run them individually go to the test class. Next to your test method there should be a diamond icon visible. When you click on it, your test will start.
Hope you find this blog useful. Please feel free to contact with me in case you have any query, suggestions. You can comment, like and follow posts. Please take a few seconds and share it with your friends!
You can request any topic related to Swift and iOS development.
Donate any small amount you think for this knowledge to grow this forum.
To Donate – paypal.me/SandeshSardar