Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
222 views
in Technique[技术] by (71.8m points)

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

I am developing an iOS project, and I need to access to the codes of the host app from the UI test target, but I found that it shows link error: undefined symbol when I tried to. I found the test host and bundle loader for this target are all empty, so I set those to my host app and could get past the link error. However, at runtime it still fails when calling XCUIApplication.launch(). Has anyone figured out how to access the codes of the host app from this UI test target? Without being able to do this, we are forced to do all UI test, which is very flaky. We definitely need to have non-UI steps in test scenarios. I am using Swift for my project.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...