App & Browser Testing Made Easy

Give your users a seamless experience by testing on 3000+ real devices and browsers. Don't compromise with emulators and simulators

Home Guide Getting Started with XCUITest : UI Automation Framework on iOS

Getting Started with XCUITest : UI Automation Framework on iOS

By Akshay Pai, Community Contributor -

Automation testing on iOS applications has been highly standardized by Apple. Apps undergo rigorous checks before they receive approval to appear on the App Store. They must abide by guidelines and strictly implemented rules from Apple.

It is imperative that iOS developers build and maintain automation test cases to test apps that align with Apple’s regulations. They ensure that successive deployments of an app are working fine and operate within the guidelines. To do this, developers must use XCUITest – a sub-framework within the XCTest Testing framework. This article will discuss the role of automation, XCUITest itself, and how to get started with it.

UI Automation Testing on iOS

UI Automation Testing is a relatively recent practice, and frameworks like XCUITest allow testers to develop clean automation test suites for an app’s UI.

1. Saving Time and Money

Advantages of shifting to UI Automation on iOS

Image Credit

From the graph above, readers can infer how the seemingly high initial investment of time and resources for automation pays off over time. This metric can also be easily extrapolated to UI testing.  The UI is the main point of interaction and it’s critical that it works as expected.

2. Code Automation isn’t Enough

There is a good chance that a developer is using third party frameworks like Appium for testing code. One might also be wondering if UI testing is even required as they have been testing their code.

The answer is YES. Developers absolutely need to perform UI testing. Code test cases check the behavior and to some extent, even the app’s interaction with other modules. However, developers do not have any metric on how each development sprint cycle or new builds impacts the UI functionality. The UI might take more than expected time to load, or some changes might have rendered UI components non-functional. Test Automation Frameworks like Appium do not help developers automatically test these scenarios.

A previous article on Appium vs XCUITest delves into the primary distinguishing features that will help readers understand why it is necessary to use both for testing iOS applications.

3. iOS XCUITest and XCTest

If someone tries to search for XCUITest, there is a good chance they might not find it right away. The reason for this is that XCUITest is a part of the XCTest framework from Apple. However, it’s still an independent module for testing user interfaces, and in this article we will offer a beginner’s guide on how to use it.

Try iOS App Testing for Free

Basics of XCUITest

Mobile app testing, and more specifically, app UI testing involves checking how the interface behaves when user actions are performed and then compares results with expected outcomes. Here, testers try to replicate exactly how a user would interact with the application and validate the state of the UI. XCUITest allows them to write test cases for these purposes using two fundamental concepts.

1. XCTest

As discussed earlier, XCTest is the base testing framework. It provides testers with the ability to write test cases with both Objective-C and Swift. Testers can write UI test classes, methods, and test targets and perform an assert to validate the behavior. Lastly, it is completely integrated with Xcode.

2. Accessibility

Using the Accessibility technology, QAs can also test and modify the app’s behavior to check how it would perform when people with disabilities use the app. In this case, UI test cases will use the functions provided by the Accessibility core to execute and validate tests.

APIs for Writing UI Automation Test Cases

To test the UI, the framework comes with three special classes that testers have to use while writing test cases.

1. XCUIApplication

Every time testers need an automated test to run, they need to specify which app needs to be launched as the first step. They need to create an instance of the XCUIApplication and call the launch method on it. This method is used to launch the app before testing starts and also used to terminate the app once testing is complete.

2. XCUIElement

The UI is made up of a set of independent UI components. The XCUIElement class of the XCUITest framework allows the test case to take control of any UI Element. This Class provides the methods to perform actions on the UI component. These actions are similar to user interaction with the app – tapping, swiping, long presses, or even rotating a component. It also allows testers to navigate the UI element hierarchy within the views.

3. XCUIElementQuery

To find a UI element (the XCUIElement) in the Interface, testers will be using the XCUIElementQuery methods. Once the application is launched and initialized, the context will carry all the information of the UI. This class will allow testers to search for a particular element on that UI and perform some action on it.

Writing a Test Case using XCUITest Framework: Example

Let’s consider a sample test case and analyze its various components.

import XCTest

class SampleXCUITest: XCTestCase {

override func setUp() {
super.setUp()
continueAfterFailure = false
XCUIApplication().launch()
}


func checkText() {
let appObj = XCUIApplication()
appObj.otherElements.containing(.image, identifier:"XCUITest Tutorial").element.tap()
appObj.buttons["Learn More"].tap()
XCTAssert(appObj.staticTexts["Automation Test iOS"].exists)
}

This test suite has one test case in it. It begins by creating a sample test class called SampleXCUITest. Step 1 is to override the setup() which launches the iOS app.

The test case here is called checkText(). Through this test case, the code is using the XCUITest framework to perform the following:

  1. Find an image within the UI with an identifier called “XCUITest Tutorial” and then perform a tap action on it.
  2. Next, find a button on the UI with the text “Learn More” and perform another tap action on it.
  3. Finally, check if the tap action leads to the text “Automation Test iOS” to appear on the UI.

When the test case is run, it executes the test described above and the XCTAssert method will validate if the final text is visible. If it does, then the automated test passes. Else, it fails.

Run iOS App Automation Test for Free

Documenting Test Case Results

Running UI test cases is not just about finding out what succeeded or failed. As UI elements are involved, testers must be able to see what went wrong, when it went wrong and at what point in the code the test case failed. The XCUITest framework provides multiple options to document these scenarios.

1. Screenshots

The XCUITest framework provides a protocol called “XCUIScreenshotProviding”. This allows testers to capture a screenshot on an XCUIElement or an XCUIScreen object. This can be used when a test fails. The screenshot can be stored on file, and used to document the bug leading to test failure. To capture a screenshot, call the screenshot() method on one of the instances mentioned above.

2. Recording

XCUITest Framework Recording

Image Source

The red button in the above image is the record button on Xcode. Upon pressing this option, Xcode will first compile the test cases and start running it on the type of device specified by the tester. Options include running tests on simulators or real devices. The recording will show all the actions that were performed on the UI.

Wrapping Things Up

XCUITests as an iOS testing framework is the iOS counterpart to Android app automation testing. It proves simple interfaces to write test cases that perform any action on the UI and then document the actions as well as results.

Apple’s documentation on User Interface Testing will offer more details on the classes and methods that testers can use to automate UI testing for an iOS app. As seen earlier, the initial investment in terms of cost and time might seem a bit high. However, over time, the ROI will become evident, making automation an indispensable part of every software development lifecycle. So, it is highly recommended to perform app automation testing using XCUITest as the framework for iOS apps.

Frequently Asked Questions

1. What is XCUITest?

XCUITest is a framework provided by Apple for UI testing of iOS apps. It allows developers to write tests that automate user interaction with the app and validate its behavior and user interface. XCUITest tests can be run in Xcode on the simulator or on a connected physical device.

2. What kind of framework is XCUITest?

XCUITest is a UI testing framework.

3. Is XCUITest open source?

No, XCUITest is not open source. It is a proprietary framework provided by Apple for testing iOS apps. It is included as part of the Xcode development environment, which is only available for MacOS.

4. What is XCUITest driver?

The XCUITest driver is a component of the XCUITest framework that allows developers to automate the testing of their iOS apps. The driver provides an API that allows tests to interact with the app, simulating user interactions and verifying the app’s behavior and user interface. The driver communicates with the Xcode development environment and the underlying iOS or tvOS operating system to execute tests and return results. The XCUITest driver is an essential part of the XCUITest framework and is used by developers to write and run automated tests for their iOS apps.

Tags
Appium Automation Testing Mobile App Testing

Featured Articles

Top 5 iOS Automated Testing Frameworks

Getting Started with Espresso – Android UI Automation

Curated for all your Testing Needs

Actionable Insights, Tips, & Tutorials delivered in your Inbox
By subscribing , you agree to our Privacy Policy.
thank you illustration

Thank you for Subscribing!

Expect a curated list of guides shortly.