ArielSD
ArielSD

Reputation: 909

How To Change Client Configuration In XCode Simulator For Automated UI Testing

After writing UI tests with XCTest, I'm trying to change the server the build points to in the Settings app in the simulator. This is so automated tests run in the right place. Is there a way to do this with accessibility and UI testing code?

How can I change this URL?

Edit: @oletha 's answer is perfect. Here's the solution I used in Objective-C:

I set the launch argument to be configServer=@"URL"

for (NSString *launchArgument in [[NSProcessInfo processInfo] arguments]) { if ([launchArgument hasPrefix:@"configServer"]) { return [launchArgument componentsSeparatedByString:@"="].lastObject; } }

Upvotes: 2

Views: 488

Answers (2)

Oletha
Oletha

Reputation: 7659

You can send a launch argument to the app by setting the launchArguments property before launching the app. You can then set up your app to read the launch argument during launch and set the URL of your mock server when it is launched with that launch argument.

// test code
let app = XCUIApplication()
app.launchArguments = ["UITESTS"]
app.launch()

// app code
if NSProcessInfo.processInfo().arguments.contains("UITESTS") {
    // set different server URL
}

You probably want to add the URL switching code in applicationDidFinishLaunching on your app delegate.

Upvotes: 2

Tomas Camin
Tomas Camin

Reputation: 10096

As per Xcode 8.2 you cannot automate UI outside your own app's target, so you cannot interact with the Settings app on the device. I feel this is a bug and filed a radar (http://www.openradar.me/27802551), you might want to do the same.

I imagine the URL you're trying to change is stored somewhere in your preferences. If that's the case you could change it dynamically during the execution of your tests using SBTUITestTunnel. This is a 3rd party library that allows to interact with your application from UI Tests, enabling network mocking and monitoring, data injection and more. For your use case it allows to easily update NSUserDefaults.

Upvotes: 0

Related Questions