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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32988241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:00:28  来源:igfitidea点击:

How to access launchEnvironment and launchArguments set in XCUIApplication, running UI tests in XCode?

iosxcodeswiftui-testing

提问by bogen

I've tried setting attributes in the XCUIApplicationinstance, in my UI Tests setUp()

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

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

in didFinishLaunchI've tried to show these on screen when I run my UITests

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()
        }
    }

But I can't seem to be able to find the arguments and environment I set. Anyone know how to get a hold of them?

但是我似乎无法找到我设置的参数和环境。有谁知道怎么抓他们?

回答by Joey C.

If you set launchArgumentsin a UI Test (Swift):

如果您launchArguments在 UI 测试 (Swift) 中设置:

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

Then read them in your App using:

然后使用以下命令在您的应用程序中阅读它们:

swift 2.x:

快速 2.x

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

Swift 3.0

斯威夫特 3.0

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

To set environment variables, use launchEnvironmentand NSProcessInfo.processInfo().environment, respectively, instead.

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

回答by Nycen

Building on Joey C.'s answer, I wrote a small extension to avoid using raw strings in the app. This way you avoid any typo issue and get autocompletion.

基于 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")
    }
}

This way you can use

这样你就可以使用

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

Going further, the various arguments should probably go into an enum that could be reused in the UITest when setting the launchArguments.

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

回答by AlekseiPetrovski

Here is example with launchArguments and Objective-C:

这是带有 launchArguments 和 Objective-C 的示例:

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

Swift:

迅速:

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

回答by Jessedc

It's also interesting to note that arguments passed to XCUIApplication.launchArgumentsare also available from UserDefaults.

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

In your XCTestCase

在您的 XCTestCase 中

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

In your target under test

在您的测试目标中

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

From here you could create extensions on UserDefaultsto provide handy run time toggles.

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

This is the same mechanism the Xcode schemes "arguments passed on launch" uses. Launch arguments and their effect on UserDefaultsare documented under Preferences and Settings Programming Guide.

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

回答by Avi Cohen

For launch arguments, pass them as two separate arguments:

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

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

Taken from here.

取自这里

回答by Hola Soy Edu Feliz Navidad

Bear in mind a few details. Firstly, XCUIAppliction is not a singleton, therefore, if you call XCUIApplication().arguments.append("myargument")and then you call XCUIApplication().launch(), it won't send the arguments. Check it here.

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

Second, if you modify the arguments after launching the app it won't work, it will send the new arguments to the next execution.

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

回答by Ceetn

I'm only aware of how this works in Objective-C

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

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

回答by Maetschl

If you need pass the environment variablesfrom your schema to XCUITes, modify the XCTestCase -> app.launchEnvironment object, on each test class of this way:

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

Swift 3

斯威夫特 3

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