As you may know Xcode provides a handy utility for recording UI tests for iOS apps. Despite the testing world is full of record-and-playback tools that provide access to UI elements, their details and characteristics, and provide full information about IDs, user interface interactions done on those, and so on. For example, Appium inspector is one of those tools that quickly provide you with all information about the underlying UI elements.
The User Interface testing for iOS apps provides the way to find and interact with the actual UI of the app, verify that all UI element characteristics are designed and implemented correctly, and navigate through the UI element hierarchy for an app.
What is XCUITest and how are UI tests done with Xcode for iOS app testing? The UI tests use XCTest framework as a basis, as well as UIAccessibility. UIAccessibility is integrated with UIKit and AppKit and provides an API for UI behavior tuning and what is used externally from the app/tests. Do not miss this complete XCTest ebook to get started with this iOS’s hot framework.
As XCTest and UI test recorder are integral part of Xcode, setting up and implementing UI tests with it is pretty straightforward. Even using Xcode Server and xcodebuild is an option to use.
Xcode UI Test Recorder and What Is It Used For
The user interface testing provides lots of information about behavior, potential flaws in design, and especially user experience. Automating the testing effort on variety of different iOS devices is simple and straightforward and Xcode’s UI test recording makes it fast to create UI specific tests for your app. You can use Xcode UI Test Recorder to:
To inspect and insight UI elements and their hierarchy in app.
The developer of an application typically knows what UI elements have been built into app, but for testers and users of record-and-playback tools this is not always clear. The whole point of using record-and-playback tools is that it makes creation of test script/UI-specific component interactions fast but also provides the higher level of abstraction of the whole UI. This way testers or other members of team do not need specific information about native UI components but all this is accessible and available through tool.
All UI details quickly traceable.
The record-and-playback tools provide quickly all details about the application’s UI layers, components and element-specific details. These details include name, description, value and even code-specific attributes. Take xpath locators as an example.
To record all manual interactions for the app.
Record-playback tools allow accurate (as user means those) interactions to be recorded and the right test script generated out of those actions. Code itself in Xcode is quick to edit and user can do tweaks on recorded piece of code before running those on simulator, or preferably, on multiple devices simultaneously.
XCUITest – What Are XCUIApplication and XCUIElement
Implementing user interface tests for iOS apps with XCUITest goes the same way as unit tests are done with XCTest. There are basically no differences in the programming model and the methodology used is pretty much the same as with XCTestCase.
XCUIApplication is basically a proxy for an app that can be launched and terminated. User can tell the application to run in testing mode by defining app as a “Target Application” in Xcode target settings.
// Objective-C XCUIApplication *app = [[XCUIApplication alloc] init]; // Swift let app = XCUIApplication()
XCUIElement is the actual UI element in an iOS application. With iOS applications, XCUIElement provides all the basics symbols and functions for UI element interactions. For example, gestures with XCTest include clicking UI elements (normal, double, pressing), interacting with the screen (swipe, pinch, zoom, rotate etc.).
The functions used with current XCUIElement are as follows:
// Click-based functions tap() doubleTap() twoFingerTap() tap(withNumberOfTaps: UInt, numberOfTouches: UInt) press(forDuration: TimeInterval) press(forDuration: TimeInterval, thenDragTo: XCUIElement) // Generic UI interactions swipeLeft() swipeRight() swipeUp() swipeDown() pinch(withScale: CGFloat, velocity: CGFloat) rotate(CGFloat, withVelocity: CGFloat)
The XCUIElement is constructed using the actual user interface elements on the screen. XCUIElement inherits from NSObject and is used as follows:
XCUIApplication *app = [[XCUIApplication alloc] init]; XCUIElement *masterNavigationBar = app.navigationBars[@"Master"]; XCUIElement *editButton = masterNavigationBar.buttons[@"Edit"];
In order to perform any interaction (tap on this example) on a UI element the following UI interactions (as listed above) can be used:
// Objective-C [masterNavigationBar.staticTexts[@"Master"] tap]; [editButton tap];
‘tap’ would perform a click on those elements.
The Basics of Using Xcode’s UI Test Recorder
The user interface testing features in Xcode include the UI test recorder which allows test script generation and mocks all interactions that user does when the test script is getting recorded. Seeing this sort of UI test recorder in Xcode is definitely a big plus for Xcode environment and it – for sure – makes creating tests much easier.
First, make sure you highlight a method that you want to record your test on. The record button can be found on bottom of the main editor screen and it will turn red (= test is recordable):
After you click the recording button the application will be compiled and started (on simulator or physical devices, based upon your preferences). Now, interact with the application’s UI will get all steps recorded and generated as Objective-C test snippet. It could look something like this:
Note that if you haven’t highlighted the test method you want to record, the record button stays gray.
To run your recorded UI test simply hit the Test (Product -> Test) and your test will be executed against the application. After the run, you are provided with some of useful information about the run and some details of potential failures. As Xcode itself doesn’t provide a lot of that useful information for running this type of UI test on physical devices, it’s always recommended to run more specific tests on mobile app testing service.
Happy XCUITest Recording Folks!
Get all essentials about XCTest framework and learn how to get started with it for cloud testingDownload