Introduction
XCUITest is used to write User Interface (UI) test cases to validate the UI functionality but only some people know that it can be used to capture screenshots that we upload on the apple store while publishing an application.
Initially, it might not sound like a big deal as capturing a screenshot manually takes only a few seconds, using a simulator or an iOS device. Consider a scenario where your mobile application supports 25 different languages. The requirement is to upload screenshots to the apple store for all languages. If we calculate the total number of screenshots to be uploaded, the count comes to 625. Now, it sounds interesting as manually taking 625 screenshots is a time-consuming process. This is where XCUITest comes to rescue us and help us automate the process of taking screenshots. Let us take a deep dive into how it can be achieved.
A Quick Glance Over the Process
We need to follow the steps below:
- Write a XCUITest that can take a screenshot of the desired screen. It is recommended to write separate test cases for each screen that we want to capture.
- Create a new test plan and add configuration for each language.
- Run test cases and export all screenshots for test logs.
Writing a UI Test
An application has multiple screens and depending on the screen we want to take a screenshot of a test case is written. The image below shows a sample code to capture a screenshot of the home screen on the launch of the application. The test case should be written in a way that all the necessary data and the UI component are displayed and then we can capture a screenshot.
Setting up a Test Plan
To create additional test plans, do the following:
- Choose Product > Test Plan > New Test Plan
- Enter a name for the new test plan.
- Choose a location to save the test plan.
- Click Create.
Configuring a Test Plan
Select the test plan and then select the Configuration tab, where, by default, we have one shared setting configuration set. This shared setting is a Global setting for the new configuration created. The next step is to create a new configuration by selecting “+”. We need to create a new configuration for each language that we support in the application. Please refer the image below:
Note: For the German configuration, we have selected the application language as German. Similarly, for each configuration, we have a different application language, and we can change other parameters depending upon our requirement.
Add a Test Plan to a Scheme
You can associate a test plan with more than one scheme, to get the same test cases and methods included with the same configurations in multiple schemes. To add an existing test plan to a scheme, do the following:
- Choose Product > Test Plan > Manage Test Plans
- Click the Add button below the list of the test plans.
- Choose Add existing Test Plan.
- Select the test plan you want to add to the scheme.
Run Test Cases to See the Magic
Once everything is set up, it’s time to run the test cases. All test cases are run for each configuration. In our cases, we have four configurations i.e., English, French, German, and Korean. In the image below, we can see that our test ran four times in the respective language and captured a screenshot of each language.
Conclusion
In conclusion, XCUITest emerges as a formidable ally of the iOS developers in their pursuit of automating screenshot capture. Its robust capabilities not only streamline the tedious process of manually capturing screenshots but also offer a comprehensive solution to ensure visual consistency and accuracy across diverse screen resolutions and device types. By harnessing the power of XCUITest, developers can significantly enhance efficiency, accelerate release cycles, and elevate the overall quality of their iOS applications. As automation continues to redefine the landscape of software development, XCUITest stands at the forefront, empowering developers to navigate the complexities of app testing with confidence and precision, ultimately delivering exceptional user experiences in the ever-evolving world of iOS development.