How to access launchEnvironment and launchArguments set in XCUIApplication, running UI tests in XCode?

IosXcodeSwiftUi Testing

Ios Problem Overview


I've tried setting attributes in the XCUIApplication instance, in my UI Tests setUp()

let app = XCUIApplication()
app.launchEnvironment = ["testenv" : "testenvValue"]
app.launchArguments = ["anArgument"]
app.launch()

in didFinishLaunch I've tried to show these on screen when I run my UITests

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    if launchOptions != nil {
        for (key, value) in launchOptions! {  
            let alertView = UIAlertView(title: key.description, message: value.description, delegate: nil, cancelButtonTitle: "ok")
            alertView.show()
        }
    }

But I can't seem to be able to find the arguments and environment I set. Anyone know how to get a hold of them?

Ios Solutions


Solution 1 - Ios

If you set launchArguments in a UI Test (Swift):

let app = XCUIApplication()
app.launchArguments.append("SNAPSHOT")
app.launch()

Then read them in your App using:

swift 2.x:

if NSProcessInfo.processInfo().arguments.contains("SNAPSHOT") {
   // Do snapshot setup
}

Swift 3.0

if ProcessInfo.processInfo.arguments.contains("SNAPSHOT") {
}

To set environment variables, use launchEnvironment and NSProcessInfo.processInfo().environment, respectively, instead.

Solution 2 - Ios

Building on Joey C.'s answer, I wrote a small extension to avoid using raw strings in the app. This way you avoid any typo issue and get autocompletion.

extension NSProcessInfo {
    /**
     Used to recognized that UITestings are running and modify the app behavior accordingly
     
     Set with: XCUIApplication().launchArguments = [ "isUITesting" ]
     */
    var isUITesting: Bool {
        return arguments.contains("isUITesting")
    }
    
    /**
     Used to recognized that UITestings are taking snapshots and modify the app behavior accordingly
     
     Set with: XCUIApplication().launchArguments = [ "isTakingSnapshots" ]
     */
    var isTakingSnapshots: Bool {
        return arguments.contains("isTakingSnapshots")
    }
}

This way you can use

if NSProcessInfo.processInfo().isUITesting {
   // UITesting specific behavior,
   // like setting up CoreData with in memory store
}

Going further, the various arguments should probably go into an enum that could be reused in the UITest when setting the launchArguments.

Solution 3 - Ios

It's also interesting to note that arguments passed to XCUIApplication.launchArguments are also available from UserDefaults.

In your XCTestCase

let app = XCUIApplication()
app.launchArguments.append("-ToggleFeatureOne")
app.launchArguments.append("true")
app.launch()

In your target under test

UserDefaults.standard.bool(forKey: "ToggleFeatureOne") // returns true

From here you could create extensions on UserDefaults to provide handy run time toggles.

This is the same mechanism the Xcode schemes "arguments passed on launch" uses. Launch arguments and their effect on UserDefaults are documented under Preferences and Settings Programming Guide.

Solution 4 - Ios

Here is example with launchArguments and Objective-C:

if ([[NSProcessInfo processInfo].arguments containsObject:@"SNAPSHOT"]) {
        //do snapshot;
}

Swift:

    let arguments = ProcessInfo.processInfo.arguments
    if arguments.contains("SNAPSHOT") {
        //do snapshot
    }

Solution 5 - Ios

For launch arguments, pass them as two separate arguments:

let app = XCUIApplication()  
app.launchArguments.append("-arg")  
app.launchArguments.append("val")  
app.launch()

Taken from here.

Solution 6 - Ios

Bear in mind a few details. Firstly, XCUIAppliction is not a singleton, therefore, if you call XCUIApplication().arguments.append("myargument") and then you call XCUIApplication().launch(), it won't send the arguments. Check it here.

Second, if you modify the arguments after launching the app it won't work, it will send the new arguments to the next execution.

Solution 7 - Ios

I'm only aware of how this works in Objective-C

NSDictionary *environment = [[NSProcessInfo processInfo] environment];

Solution 8 - Ios

If you need pass the environment variables from your schema to XCUITes, modify the XCTestCase -> app.launchEnvironment object, on each test class of this way:

Swift 3

override func setUp(){
    app.launchEnvironment = ProcessInfo.processInfo.environment
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbogenView Question on Stackoverflow
Solution 1 - IosJoey C.View Answer on Stackoverflow
Solution 2 - IosArnaudView Answer on Stackoverflow
Solution 3 - IosJessedcView Answer on Stackoverflow
Solution 4 - IosAlekseiPetrovskiView Answer on Stackoverflow
Solution 5 - IosAvi CohenView Answer on Stackoverflow
Solution 6 - IosHola Soy Edu Feliz NavidadView Answer on Stackoverflow
Solution 7 - IosCeetnView Answer on Stackoverflow
Solution 8 - IosMaetschlView Answer on Stackoverflow