Xcode UI testing - login/logout with stored credentials

Viewed 3206

I want to run functional (UI) tests for the login procedure in my iOS app (Xcode 7.2.1).

The app's behaviour is that upon successful login, user credentials are stored in order to automatically login (without showing the login screen) in the next launches.

So I set up a sequence of UI events in the login screen to make the login test pass on the first time the app launches in the iOS Simulator. However, next times I run my tests will fail, since the login screen doesn't even show up as expected.

I see two options here, none of them seem to fit well:

  1. Reset iOS Simulator's content and settings with a script before each time my tests run. I tried adding a Run Script phase in the test target's Build Phases with: xcrun simctl shutdown booted && xcrun simctl erase all && killall "Simulator", and it doesn't seem to work (Simulator app doesn't launch and tests get stuck).
  2. Include in the -(void)tearDown some code to clear the stored user credentials. This option is not good either as not only it's run between each test method (not per test launch), but also it seems like I don't have access to the AuthManager class that I use to clear user's credentials.

What do you do when UI-testing login procedures like that?

3 Answers

When you run your application from XCTestCase you could use something like this

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

And in AppDelegate method

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool

if CommandLine.arguments.contains("--uitesting") {
    clear()
} 

Run this in first test when you need to login. In next test you could clear data from launchArguments.

app.launchArguments = []
Related