13

In Xcode 9.2, for my iOS app, I have a Scheme for running my UI Test suite. In this scheme, under the "Test" tab, I have both environment variables and arguments defined. When my tests run, these variables aren't available to my app code.

However, I am able to programmatically set both the arguments and the launchEnvironment in the test code, and those ones are available to the app while it's running under test.

I also have environment variables and arguments set in the "Run" tab, and those are available when I merely run the app in the simulator. They are not available when I run my UI tests, regardless of whether I have "Use the Run action's arguments and environment variables" toggled on.

To see the values of the args and env vars, I have a breakpoint set in my applicationDidFinishLaunching, and I po ProcessInfo.processInfo.environment and po ProcessInfo.processInfo.arguments.

Am I setting something incorrectly here?

enter image description here

commanda
  • 4,841
  • 1
  • 25
  • 34
  • Suggest that you break through to verify that the code is running, and through the 'p print' command line output – Tim Jan 11 '18 at 06:25

1 Answers1

31

Launch arguments and launch environments provided in the scheme editor are passed only to UI test runner. You can read those values only in UI test classes.

If you want to pass those values to the application itself you have to do it in the code:

let app = XCUIApplication()
app.launchArguments = ["amandaArgument"]
app.launchEnvironment = ["AMANDA_UI_TESTING": "Value"]
app.launch()
Bartosz Janda
  • 1,611
  • 11
  • 10
  • When you place the environment in the test setup, it doesn't run when recording UI interaction. Is there any way to have the application launch with a setting based on the scheme? – Robin Daugherty Apr 14 '18 at 17:28
  • Oh, this is kind of obvious. The answer is to place the `launchEnvironment` variable as described here, and also add it to the Test action's Environment Variables. The environment variable will be given to the UI test runner, but when recording UI tests, it will be given to the application instance instead. – Robin Daugherty Apr 14 '18 at 17:36
  • You can write in in the XCUITest test method – Bartosz Janda Jan 09 '19 at 14:56
  • Do you have a reference for this answer? I would like to read more about it. – Brandt Aug 23 '19 at 00:32
  • I've only official [documentation](https://developer.apple.com/documentation/xctest/xcuiapplication), WWDC, and [AutoMate](https://github.com/PGSSoft/AutoMate) project. – Bartosz Janda Aug 23 '19 at 12:51