如何访问在XCUIApplication中设置的launchEnvironment和launchArguments,在XCode中运行UI测试?

bog*_*gen 42 xcode ui-testing ios swift

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

let app = XCUIApplication()
app.launchEnvironment = ["testenv" : "testenvValue"]
app.launchArguments = ["anArgument"]
app.launch()
Run Code Online (Sandbox Code Playgroud)

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()
        }
    }
Run Code Online (Sandbox Code Playgroud)

但我似乎无法找到我设定的论点和环境.有谁知道怎么抓住他们?

Joe*_* C. 57

如果你设置launchArguments了UI测试(Swift):

let app = XCUIApplication()
app.launchArguments.append("SNAPSHOT")
app.launch()
Run Code Online (Sandbox Code Playgroud)

然后使用以下内容在您的应用中阅

swift 2.x:

if NSProcessInfo.processInfo().arguments.contains("SNAPSHOT") {
   // Do snapshot setup
}
Run Code Online (Sandbox Code Playgroud)

Swift 3.0

if ProcessInfo.processInfo.arguments.contains("SNAPSHOT") {
}
Run Code Online (Sandbox Code Playgroud)

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

  • 在Xcode 7.3下,这似乎不起作用.我已经尝试过设置launchArguments然后直接读取它们.我总是回来和空阵.即使我在下一行做了.看起来他们可能会被打破.对于launchEnvironment也是如此. (6认同)
  • 当Xcode中的`Record UI Test`时,它不会运行`setUp()`.它误导我上面的代码不起作用.但是再次运行UI测试(不是"记录UI测试"模式),它可以工作. (4认同)

Nyc*_*cen 16

基于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")
    }
}
Run Code Online (Sandbox Code Playgroud)

这样你就可以使用了

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

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


Ale*_*ski 8

以下是launchArguments和Objective-C的示例:

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

迅速:

    let arguments = ProcessInfo.processInfo.arguments
    if arguments.contains("SNAPSHOT") {
        //do snapshot
    }
Run Code Online (Sandbox Code Playgroud)


Jes*_*edc 7

有趣的是,传递给的参数XCUIApplication.launchArguments也可以从获得UserDefaults

在您的XCTestCase中

let app = XCUIApplication()
app.launchArguments.append("-ToggleFeatureOne")
app.launchArguments.append("true")
app.launch()
Run Code Online (Sandbox Code Playgroud)

在被测目标中

UserDefaults.standard.bool(forKey: "ToggleFeatureOne") // returns true
Run Code Online (Sandbox Code Playgroud)

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

这与Xcode方案“启动时传递的参数”所使用的机制相同。启动参数及其对它们的影响UserDefaults记录在“首选项和设置编程指南”下


Avi*_*hen 6

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

let app = XCUIApplication()  
app.launchArguments.append("-arg")  
app.launchArguments.append("val")  
app.launch()
Run Code Online (Sandbox Code Playgroud)

取自这里.


Hol*_*dad 6

请记住一些细节。首先, XCUIAppliction 不是单例,因此,如果您调用XCUIApplication().arguments.append("myargument")然后调用XCUIApplication().launch(),它不会发送参数。在这里检查

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


Cee*_*etn 4

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

NSDictionary *environment = [[NSProcessInfo processInfo] environment];
Run Code Online (Sandbox Code Playgroud)