bog*_*gen 42 xcode ui-testing ios swift
我尝试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()
        }
    }
但我似乎无法找到我设定的论点和环境.有谁知道怎么抓住他们?
Joe*_* C. 57
如果你设置launchArguments了UI测试(Swift):
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") {
}
要设置环境变量,请分别使用launchEnvironment和   NSProcessInfo.processInfo().environment.
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")
    }
}
这样你就可以使用了
if NSProcessInfo.processInfo().isUITesting {
   // UITesting specific behavior,
   // like setting up CoreData with in memory store
}
更进一步,各种参数应该进入枚举,可以在设置launchArguments时在UITest中重用.
以下是launchArguments和Objective-C的示例:
if ([[NSProcessInfo processInfo].arguments containsObject:@"SNAPSHOT"]) {
        //do snapshot;
}
迅速:
    let arguments = ProcessInfo.processInfo.arguments
    if arguments.contains("SNAPSHOT") {
        //do snapshot
    }
有趣的是,传递给的参数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记录在“首选项和设置编程指南”下。
对于启动参数,将它们作为两个单独的参数传递:
let app = XCUIApplication()  
app.launchArguments.append("-arg")  
app.launchArguments.append("val")  
app.launch()
取自这里.
我只知道它在 Objective-C 中是如何工作的
NSDictionary *environment = [[NSProcessInfo processInfo] environment];
| 归档时间: | 
 | 
| 查看次数: | 17193 次 | 
| 最近记录: |