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

6tqwzwtp  于 2023-05-16  发布在  Swift
关注(0)|答案(9)|浏览(149)

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

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

但我似乎找不到我设置的论点和环境。有人知道怎么弄到吗?

vjhs03f7

vjhs03f71#

如果你在UI Test(Swift)中设置了launchArguments

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") {
}

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

8qgya5xd

8qgya5xd2#

同样有趣的是,传递给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的影响在“Preferences and Settings Programming Guide(首选项和设置编程指南)”中有说明。

kpbpu008

kpbpu0083#

基于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中重用该枚举。

34gzjxbg

34gzjxbg4#

下面是launchArguments和Objective-C的例子:

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

斯威夫特:

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

o4tp2gmn5#

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

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

来自here

u4vypkhs

u4vypkhs6#

记住一些细节。首先,XCUIApplication不是单例,因此,如果您调用XCUIApplication().arguments.append("myargument"),然后再调用XCUIApplication().launch(),它不会发送参数。Check it here
第二,如果你在启动应用程序后修改参数,它将无法工作,它将把新的参数发送到下一次执行。

pwuypxnk

pwuypxnk7#

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

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

jgzswidk8#

如果你需要将环境变量从你的schema传递给XCUITes,修改XCTestCase -> app.launchEnvironment对象,在每个测试类上,这样:

*Swift 3酒店,纽约

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

ckx4rj1h9#

1-创建ProcessInfo的扩展

extension ProcessInfo {

    public enum LaunchArgument: String {
        case uiTestSampleData
        case uiTestStagingData
        case none
    }

    public var launchArgument: LaunchArgument {
    
        if arguments.contains(LaunchArgument.uiTestSampleData.rawValue) {
            return .uiTestSampleData
        }
    
        if arguments.contains(LaunchArgument.uiTestStagingData.rawValue) {
            return .uiTestStagingData
        }
    
        return .none
    }

}

2-修改数据源init。在本例中,我使用的是莫亚,因此我修改了提供程序init。在init中检查参数并模拟sameplData参数的响应,而不是正常地初始化REST API。

public init(provider: MyProvider<MyStoreApi> = MyProvider<MyStoreApi>()) {
    
    switch ProcessInfo.processInfo.launchArgument {
        
    case .uiTestSampleData:
        let provider = MyProvider<MyStoreApi>(stubClosure: MoyaProvider.immediatelyStub)
        self.provider = provider
        
    case .none, .uiTestStagingData:
        self.provider = provider
    }

}

3-在UI测试中添加参数

app.launchArguments.append("uiTestSampleData")

相关问题