This post is from the Experitest blog and has not been updated since the original publish date.
How to Get Started with XCUITest (iOS)
In the age of digital transformation, applications are growing as the dominant form of digital interactions. Today, there are as many applications as there are consumer demands for digitally-enabled services. Applications are growing in importance due to their easy access and popularity among digital consumers. This prompts forward-looking companies to ensure that their integrated marketing communication (IMC) strategies do not miss out on this valuable tool. As a result, we see an increasing number of applications being developed, tested, and rolled out every day. It also leads to the growth of tools for development and testing like XCUITest and Espresso.
While this is certainly a growing opportunity for application developers, it comes with some daunting challenges. One of the key areas throttling the progress of application development is testing. Companies failing to deploy Agile and DevOps practices often find it quite difficult to meet the growing demand for applications without compromising quality. With DevOps, Agile, and CI/CD practices, it's easy to overcome the testing bottleneck, shorten release cycles, and improve the quality of the end product. Therefore, Experitest's automation solutions are geared towards helping companies achieve efficiency and scalability without compromising the quality of their applications.
In this blog post, we will demonstrate how you can use the XCUITest framework along with SeeTest to help you automate UI testing for iOS applications.
What is XCUITest Framework?
Apple's Xcode UI Test framework is not really new, it's built on XCTest which is part of Xcode and has been around for some years now. Created for iOS apps, XCUITest is an integral part of Xcode and is Apple's official test automation framework for native apps and games created for iOS devices. Since it is tightly integrated with Xcode already, the framework supports writing test cases with subclasses, methods, and assertions. XCUITest is available and supported by Experitest platform. In the following sections, we will walk you through the steps of how you can get started with XCUITest and integrate it with testing automation platforms like SeeTest.
Benefits and Shortcomings
XCUITest is a viable choice for developers and test engineers as it's tightly coupled with their development tool and environment. Nevertheless, it's better for users to be aware of the pros and cons of this framework. On the plus side, XCUITest is easy to learn and there are no additional components required to get started. It supports native iOS language, provides an Xcode test recorder for UI test recording, and supports integrating with widely used CI systems. XCUITest is fast, stable, and provides the ability to use Xcode IDE for performance testing. However, on the minus side, the framework doesn't provide cross-platform support, has limited programming language support, and is a bit flaky. It still has some issues as far as the usability and robustness of creating iOS tests are concerned.
XCUITest vs. Appium
How to get Started with XCUITest (iOS)
If you have an existing project within Xcode and would like to add automated UI tests to it, follow the steps provided below. You can also watch our webinar where Experitest CTO, Guy Arieli, has provided a full demo of how it all works.
Create a new target.
Open your Xcode project. Go to the menu on top -> editor ->Add target
Choose a template for your new target:
Search for iOS UI Testing Bundle in the search box. -> choose iOS UI Testing Bundle from the search result -> hit Next
From the window, now you need to choose a few options for your new target including the test name. There are quite a few options, but you can just name your test (in the Product Name field) and select your target to be tested. Hit Finish (and a new target has been created).
Specify how to sign into the app
Set your provisioning profile in both the Debug field and Release field. The profile you choose would be the one you will use for the test and target application.
On the options panel towards the left, you should now be able to see your new demo test under the test name you have used.
Create a new scheme
This is basically a project configuration where we need to set the target for our project.
Go to Product menu at the top -> Scheme -> New Scheme
Choose your target and test name ("demotest" for both in the demo case)
Now, click on your test -> Edit scheme (to configure your scheme). We only need to change the following:
Go to Run Debug item on the left -> Executable ->Select your target application from within your project. -> hit the Close button.
Now hit the recording (the red dot button placed next to the Debug area) which will generate the test script and identify the UI elements for you. In the demo case, we are providing a user name and password for the XCUITest to record our test.
Note: The recording could be far from perfect. It'll require some editing and minor corrections. Make sure you go through the recording parts carefully and fix any mistakes/repetitions you notice there.
That is it. Now we are pretty much done with the configuration and can run our test.
Run the test
Go to the menu on top. Select Product -> Test
A pop up will ask you to stop the application which is running; click Stop.
Once you stop the app, a new pop up with the message "Build Succeeded" will appear. From this step on (until the test is executed), you don't have to do anything since the test will run automatically. You may, however, notice the test updating the IPA file into your emulator and launch the application.
Package the results of the test
In the final step, you will need to package the test results within GUITest so you can run your test on real devices using the Experitest cloud resources. There are two results files we need to upload to the cloud environment through the REST API. One is the test file and the other one the application file.
The process is not very difficult and you need to do a few more things which are as follows:
- Create a new folder and name it "Payload" (the name is case sensitive, so make sure the "P" is in caps).
- Put the application file in the folder you just created.
- Zip the folder, rename it if you like, (changing the name won't matter once you zip the folder), and put ".ipa" to the end of the folder name. This will convert it into an IPA file.
Now upload the two files to the cloud using Postman that will send the files through the REST API.
That is about it. Everything is all set now and as a result, your QA cloud is ready to run the test on real devices.
Xcode UI testing is a huge expansion of testing technology for iOS-based applications. It enables simulating user actions to find out if everything is working with the latest changes in codes. With XCUITest, we can fiddle with the UI elements on our apps and validate their state or position to ensure that end users do not encounter unexpected results or have poor experiences. Moreover, getting started with XCUITest is fairly easy and any iOS developer can do it without having to learn a new framework or language. Keep in mind, though, that a testing framework is as good as the insights it provides. Without a well-rounded reporting system and data obtained from real devices, XCUITest is only half the solution. With the Experitest automation platform, you can turn it into a complete package.