Xcode UI testing - login/logout with stored credentials

為{幸葍}努か 提交于 2019-12-05 07:59:40

I've ran into the same types of issues. After a bunch of bashing around my best approach was to try and keep things a little more simple. In my tear downs I always "unwind" anything that I might have done. Some times it's overkill but it's still good practice. I've found many bugs while unwinding where I might have overlooked them if I did some sort of hard reset. IE: I'll navigate back to the home page (my starting point) and if I've signed in then I'll simply just sign out. For the record, on the app side when a users signs out, their credentials are stripped.

So for example, on my SignInTests.swift classes I put all my methods in an extension in the same class file. That way I can simply call SignInTests().signIn() or SignInTests().signOut() so that I can access them from whatever other test class I might to called signOut() from.

This is my scenario that works perfectly for me. May not be the best option for you but I hope it points you in the right direction.

Our answer (at my company) was to create an extension of XCTestCase called ContainerResettingUITest. We pass in a series of additional launch arguments that override setUp (rather than tearDown) and then in main() if those launch arguments were passed in we nuke everything in the docs directory, the keychain, and the nsuserdefaults.

A somewhat nuclear option. Don't pass that launch argument by accident.

Hopefully that answers your question; I use it for exactly the same reason you seem to want it, to have totally uncontaminated sign-in and sign-up tests.

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 = []
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!