- Add "UI-Testing" to
launchArguments
before launching the app under test - On launch, check for the argument in
AppDelegate.swift
- If it exists remove everything for the app's domain from
NSUserDefaults
-
-
Save joemasilotti/74df5e13adede2e132461dfea32b441c to your computer and use it in GitHub Desktop.
Resetting NSUserDefaults in UI Testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
@UIApplicationMain | |
class AppDelegate: UIResponder, UIApplicationDelegate { | |
var window: UIWindow? | |
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { | |
resetStateIfUITesting() | |
window = UIWindow(frame: UIScreen.mainScreen().bounds) | |
window?.rootViewController = RootViewController() | |
window?.makeKeyAndVisible() | |
return true | |
} | |
private func resetStateIfUITesting() { | |
if NSProcessInfo.processInfo().arguments.contains("UI-Testing") { | |
NSUserDefaults.standardUserDefaults().removePersistentDomainForName(NSBundle.mainBundle().bundleIdentifier!) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import XCTest | |
class UITests: XCTestCase { | |
private let app = XCUIApplication() | |
override func setUp() { | |
super.setUp() | |
app.launchArguments += ["UI-Testing"] | |
app.launch() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works great in Xcode 10!