iOS - How to create UITest Environments?
Mohamed Elbana
Mobile Team Lead | Software Architect | Expert in building secure, scalable, and high-performance products
When writing UITest on iOS or any platform, you want to handle cases such as application will open as not registered user to test registration cycle or application will open as no language selected to test select language.
So that, you should create different environments for UITest to test all possible cases.
In this article, I will show you a straightforward and very simple way to create different environments for your UITest, and in this article, I will not talk about Mock data "This will be another article", I will just talk about how to create different environments.
I will use "launchArguments", launchArguments is the arguments that pass to the application on launch, The arguments can be changed, added to, or removed. Unlike Proccess, it is also legal to modify these arguments after the application has been launched.
Such changes will not affect the current launch session, but will take effect the next time the application is launched.
------------- ???? ???? -------------
Let's start...
First, you should create a UITest target and add "App.swift" file, this file will contain your environments such as the example below:
In "App.swift" file, we defined different environments depending on each others, first we defined the base environment "UI-TEST".
Then "NOT-REGISTERED-USER" if we want to test the registration cycle, and it depends on "UI-TEST".
Then "NO-LANGUAGE-SELECTED" if we want to test select language, and "NO-LANGUAGE-SELECTED" depends on "NOT-REGISTERED-USER" because the select language screen will open the first time only so the user will not create a registration.
And instead of starting your UITest functions with:
let app = XCUIApplication()
You will use one of our environments such as:
let app = App.noLanguageSelected
Example of launch app at UITest function:
领英推荐
------------- ???? ???? -------------
After that, we want to create the layer at the main application target to use environments in our code.
First, create "UITestKeys.swift" file that contains UITest environments keys as below:
Second, create a class to check which environment has been launched, The class called "UITestEnvironment" as below:
Finally, we can just check at any part in our code about the environment and create what we want to complete the UITest cycle.
Example:
In the previous example, you will check the environment which has been launched from "testSelectLanguageComponents" at UITests and, navigates to Select language screen to complete the select language cycle of UITest.
------------- ???? ???? -------------
Ending...
In the next article, I will talk about How to mock data at UITest?.
Thanks,
Mohamed Elbana