我尝试在我的UI测试XCUIApplication中的setUp()实例中设置属性

let app = XCUIApplication()
app.launchEnvironment = ["testenv" : "testenvValue"]
app.launchArguments = ["anArgument"]
app.launch()

didFinishLaunch中,我尝试在运行UITests时在屏幕上显示这些内容
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    if launchOptions != nil {
        for (key, value) in launchOptions! {
            let alertView = UIAlertView(title: key.description, message: value.description, delegate: nil, cancelButtonTitle: "ok")
            alertView.show()
        }
    }

但是我似乎无法找到我设置的参数和环境。有人知道如何捕获他们吗?

最佳答案

如果您在UI测试(Swift)中设置launchArguments,请执行以下操作:

let app = XCUIApplication()
app.launchArguments.append("SNAPSHOT")
app.launch()

然后使用以下方法在您的应用中阅读它们:

swift 2.x :
if NSProcessInfo.processInfo().arguments.contains("SNAPSHOT") {
   // Do snapshot setup
}

Swift 3.0
if ProcessInfo.processInfo.arguments.contains("SNAPSHOT") {
}

要设置环境变量,请分别使用launchEnvironmentNSProcessInfo.processInfo().environment

10-06 02:13