【问题标题】:How to access launchEnvironment and launchArguments set in XCUIApplication, running UI tests in XCode?如何访问 XCUIApplication 中设置的 launchEnvironment 和 launchArguments,在 XCode 中运行 UI 测试?
【发布时间】:2016-01-04 10:47:17
【问题描述】:

我尝试在 XCUIApplication 实例中设置属性,在我的 UI 测试 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()
        }
    }

但我似乎无法找到我设置的参数和环境。有谁知道如何获取它们?

【问题讨论】:

  • 是的,我也找不到它们——希望有更多关于这些东西的文档。很酷只是......很难进行一些好的测试。
  • 嗨,你有没有机会知道苹果网站上是否有任何使用launchArguments的例子?官方文档简直要了我的命:The arguments that will be passed to the application on launch (developer.apple.com/documentation/xctest/xcuiapplication/…)。好像从名字上看不出来

标签: ios xcode swift ui-testing


【解决方案1】:

请记住一些细节。首先,XCUIAppliction 不是单例,因此,如果你调用XCUIApplication().arguments.append("myargument") 然后你调用XCUIApplication().launch(),它不会发送参数。 Check it here.

其次,如果你在启动应用程序后修改了参数,它不会工作,它会将新的参数发送到下一次执行。

【讨论】:

  • 这是答案而不是实际解决问题并解释了原因。
【解决方案2】:

值得注意的是,传递给XCUIApplication.launchArguments 的参数也可以从UserDefaults 获得。

在您的 XCTestCase 中

let app = XCUIApplication()
app.launchArguments.append("-ToggleFeatureOne")
app.launchArguments.append("true")
app.launch()

在您的测试目标中

UserDefaults.standard.bool(forKey: "ToggleFeatureOne") // returns true

从这里您可以在 UserDefaults 上创建扩展,以提供方便的运行时切换。

这与 Xcode 方案“启动时传递的参数”使用的机制相同。启动参数及其对UserDefaults 的影响记录在Preferences and Settings Programming Guide 下。

【讨论】:

    【解决方案3】:

    这是 launchArguments 和 Objective-C 的示例:

    if ([[NSProcessInfo processInfo].arguments containsObject:@"SNAPSHOT"]) {
            //do snapshot;
    }
    

    斯威夫特:

        let arguments = ProcessInfo.processInfo.arguments
        if arguments.contains("SNAPSHOT") {
            //do snapshot
        }
    

    【讨论】:

    • 嗨!在哪里可以找到所有支持的启动参数列表?
    【解决方案4】:

    如果您需要将 环境变量 从您的架构传递到 XCUITes,请在每个测试类上修改 XCTestCase -> app.launchEnvironment 对象:

    斯威夫特 3

    override func setUp(){
        app.launchEnvironment = ProcessInfo.processInfo.environment
    }
    

    【讨论】:

      【解决方案5】:

      如果您在 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

      【讨论】:

      • 在 Xcode 7.3 下这似乎不起作用。我试过 set launchArguments 然后直接读回来。我总是回来并清空数组。即使我在下一行做。看起来他们可能被打破了。 launchEnvironment 也一样。
      • 确保您只有一个 XCUIApplication 实例并在 设置 launchArguments 后调用启动。
      • Record UI Test 在 Xcode 中时,它不会运行 setUp()。它误导了我上面的代码不起作用。但是再次运行 UI 测试(不是Record UI Test 模式),它可以工作。
      • 万一其他人像我一样因为它仍然无法工作而敲打他们的头,构建配置也必须设置为调试。如果设置为 Release,它将有空的启动参数。
      【解决方案6】:

      对于启动参数,将它们作为两个单独的参数传递:

      let app = XCUIApplication()  
      app.launchArguments.append("-arg")  
      app.launchArguments.append("val")  
      app.launch()
      

      取自here

      【讨论】:

        【解决方案7】:

        基于 Joey C. 的回答,我编写了一个小扩展以避免在应用程序中使用原始字符串。这样您就可以避免任何拼写错误并获得自动完成功能。

        extension NSProcessInfo {
            /**
             Used to recognized that UITestings are running and modify the app behavior accordingly
        
             Set with: XCUIApplication().launchArguments = [ "isUITesting" ]
             */
            var isUITesting: Bool {
                return arguments.contains("isUITesting")
            }
        
            /**
             Used to recognized that UITestings are taking snapshots and modify the app behavior accordingly
        
             Set with: XCUIApplication().launchArguments = [ "isTakingSnapshots" ]
             */
            var isTakingSnapshots: Bool {
                return arguments.contains("isTakingSnapshots")
            }
        }
        

        这样你就可以使用

        if NSProcessInfo.processInfo().isUITesting {
           // UITesting specific behavior,
           // like setting up CoreData with in memory store
        }
        

        更进一步,各种参数可能应该进入一个枚举,可以在设置 launchArguments 时在 UITest 中重用。

        【讨论】:

          【解决方案8】:

          我只知道这在 Objective-C 中是如何工作的

          NSDictionary *environment = [[NSProcessInfo processInfo] environment];
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-03-20
            • 1970-01-01
            • 1970-01-01
            • 2016-08-31
            • 1970-01-01
            • 2016-08-14
            相关资源
            最近更新 更多