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)
要设置环境变量,请分别使用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")
}
}
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中重用.
以下是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)
有趣的是,传递给的参数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
记录在“首选项和设置编程指南”下。
对于启动参数,将它们作为两个单独的参数传递:
let app = XCUIApplication()
app.launchArguments.append("-arg")
app.launchArguments.append("val")
app.launch()
Run Code Online (Sandbox Code Playgroud)
取自这里.
我只知道它在 Objective-C 中是如何工作的
NSDictionary *environment = [[NSProcessInfo processInfo] environment];
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
17193 次 |
最近记录: |