Ios 如何访问XUIApplication中设置的launchEnvironment和launchArguments,在XCode中运行UI测试?
我已经在我的UI测试Ios 如何访问XUIApplication中设置的launchEnvironment和launchArguments,在XCode中运行UI测试?,ios,xcode,swift,ui-testing,Ios,Xcode,Swift,Ui Testing,我已经在我的UI测试setUp() 在didfishlaunch中,我尝试在运行UITests时在屏幕上显示这些 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { if launchOptions != nil { for (key, value) in launchOp
setUp()
在didfishlaunch
中,我尝试在运行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()
}
}
但我似乎无法找到我设置的参数和环境。有人知道如何抓住他们吗?我只知道这在Objective-C中是如何工作的
NSDictionary *environment = [[NSProcessInfo processInfo] environment];
如果在UI测试(Swift)中设置
launchArguments
:
然后使用以下方法在应用程序中阅读:
swift 2.x:
if NSProcessInfo.processInfo().arguments.contains("SNAPSHOT") {
// Do snapshot setup
}
Swift 3.0
if ProcessInfo.processInfo.arguments.contains("SNAPSHOT") {
}
要设置环境变量,请分别使用
launchenEnvironment
和NSProcessInfo.processInfo().environment
。基于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
}
对于启动参数,将其作为两个单独的参数传递:
let app = XCUIApplication()
app.launchArguments.append("-arg")
app.launchArguments.append("val")
app.launch()
摘自。记住一些细节。首先,XUIAppliction不是单例,因此,如果调用
XUIApplication().arguments.append(“myargument”)
,然后调用XUIApplication().launch()
,它将不会发送参数
其次,如果在启动应用程序后修改参数,则该应用程序将不起作用,它会将新参数发送到下一次执行。如果需要将环境变量从架构传递到XUITES,请以以下方式在每个测试类上修改XCTESCASE->app.launchenEnvironment对象: Swift 3
override func setUp(){
app.launchEnvironment = ProcessInfo.processInfo.environment
}
还值得注意的是,传递给
xguiapplication.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
的影响记录在下。是的,我也找不到它们——希望有更多关于这方面的文档。很酷,只是。。很难进行一些好的测试。嗨,你知道苹果网站上有没有关于如何使用launchArguments
的例子吗?官方文档简直要了我的命:将在启动时传递给应用程序的参数
()。从Xcode 7.3下的名称看似乎不明显,这似乎不起作用。我试过设置launchArguments,然后直接回读它们。我总是返回空数组。即使我在下一行做。看起来它们可能坏了。launchEnvironment也是如此。请确保只有一个XUIApplication实例,并在设置launchArguments后调用launch。在Xcode中录制UI测试时,它不会运行setUp()
。它误导我上述代码不起作用。但是再次运行UI测试(而不是录制UI测试
模式),它可以工作。如果其他人像我一样因为UI测试仍然不工作而头痛,那么构建配置也必须设置为调试。如果设置为Release,它将有空的启动参数。这是实际解决问题的答案,并解释了原因。嗨!在哪里可以找到所有受支持的启动参数列表?
let app = XCUIApplication()
app.launchArguments.append("-ToggleFeatureOne")
app.launchArguments.append("true")
app.launch()
UserDefaults.standard.bool(forKey: "ToggleFeatureOne") // returns true