Accessing the host app code from the Xcode 7 UI Test target

两盒软妹~` 提交于 2019-11-28 03:42:44

I use the following technique to set values in my App before running UI tests. Useful for setting defaults or turning on networking mocks, etc. For the most part, I haven't needed to read anything out of the app yet. Everything is reflected in the UI and can be tested that way. Soon we're going to add tests to ensure the app makes certain network calls. I'm not yet sure how we'll test that from inside a UI test case.

Set arguments in your UI test

let app = XCUIApplication()
app.launchArguments = ["ResetDefaults", "NoAnimations", "UserHasRegistered"]
app.launch()

Read arguments in your app

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    var arguments = NSProcessInfo.processInfo().arguments
    arguments.removeFirst()
    print("App launching with the following arguments: \(arguments)")

    // Always clear the defaults first
    if arguments.contains("ResetDefaults") {
      destroyUserDefaults()
      clearKeychain()
    }

    for argument in arguments {
      switch argument {
      case "NoAnimations":
        UIView.setAnimationsEnabled(false)
      case "UserHasRegistered":
        Defaults.userRegistered = true
      default:
        break
    }
  }
}

Additional bonus: If you use launch arguments to configure your app, it's trivial to add flags to your Xcode scheme. For example, I have a scheme that clears all stored data, and stubs out any login attempt with a success response. I can now "login" via the simulator without hitting any servers.

Using the new UI automation framework Apple introduced in WWDC 15 it is not possible to access your app's code from the UI-Automation test. It's meant to simulate what a user would have access too, which can be frustrating at times.

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