UI Testing. Launch Arguments & Launch Environment

There are times when we need our UI Test code to pass some information to our mobile app, not by typing it into a Text Field or a Text View but by sending it as a command-line argument or as a launch environment property. To pass a command-line launch argument or an environment key-value pair values from UI Test code to app code, we can use Launch Arguments and Launch Environment. 

If you are interested in video lessons on how to write Unit tests and UI tests to test your Swift mobile app, check out this page: Unit Testing Swift Mobile App

Passing Launch Arguments

Launch arguments is an array of Strings. To pass launch arguments from UI test code to your mobile app, you will create an instance of XCUIApplication and then set an array of launch arguments using the launch arguments property. 

Launch arguments usually look like short commands. like for example: “skipSurvey” or “debugServer” and then your app code will read these arguments and act accordingly. For example, if one of the launch arguments is skipSurvey, then your app will not present a user with a survey as soon as it starts. 

let app = XCUIApplication()
app.launchArguments = ["-skipSurvey", "-debugServer"]
app.launch()

Passing Launch Environment

Launch environment works very similarly to launch arguments except that a launch environment is a dictionary of key: value pairs. 

To pass the launch environment key: value pairs, you will create a dictionary containing the environment properties and then will assign this dictionary to a launchEnvironment property on the XCUIApplication object.  

For example, you might need to test your app with different test servers. In this case, you can pass an IP address of the test server via an environment property. Another example couple is if you want to specify that when the app starts, the in-app purchases feature is enabled for this user, or that in-app advertisement is enabled.  

let app = XCUIApplication()
app.launchEnvironment = ["serverIP":"52.91.241.56",
                         "inAppPurchasesEnabled":"true",
                         "inAppAdsEnabled":"true"]
app.launch()

Reading Launch Arguments

There are a couple of ways to read Launch Arguments in your mobile app Swift code. 

One of the ways is to use the CommandLine enum. Using The command line enum we can access the arguments property which is an array of strings containing all command-line arguments. Launch arguments we have added to XCUIApplication in our test method, are in this list. 

If in our test method, we have started our application with a launch argument “-skipSurvey”, then in our app code, we can check if the CommandLine.arguments contain the “skipSurvey” argument. And if it does, then we can execute some code that will disable the survey feature. 

#if DEBUG
if CommandLine.arguments.contains("-skipSurvey") {
    print("skipping survey")
}
#endif

Notice that I have surrounded this code with an #if DEBUG statement?. This is a conditional compilation which means that the code inside of this IF statement will compile only when the mobile application is being built for debugging purposes. When we compile this code with Release configuration, the code inside of this conditional compilation IF statement will be skipped and will not be included. Which is what we want. 

Another way to read the Launch arguments is to use the Process API. It works very similar to command line arguments and allows us to read the launch arguments used for this process. The launch arguments are also an array of Strings in this case and we need to simply check if this array of Strings contains an argument we have set in our test code.

#if DEBUG
if ProcessInfo.processInfo.arguments.contains("-skipSurvey") {
    print("skipping survey from process")
}
#endif

Reading Launch Environment

We can also use the ProcessInfo API to read the launch environment key: value pairs. The environment object that we get here is a dictionary and we can use it to read the launch configuration properties we have set in our test code. 

To read an environment value with the key “signupUrl” you will use the following:

var singupURL = ProcessInfo.processInfo.environment["signupUrl"]

Leave a Reply

Your email address will not be published. Required fields are marked *

Free Video Lessons

Enter your email and stay on top of things,

Subscribe!