XCTest/Swift Automation:如何在测试前确定应用程序是否存在?

问题描述 投票:0回答:1

我正在一个 iOS 应用程序上使用 XCTest 进行 UI 自动化,该应用程序具有不同的捆绑包 ID,具体取决于它的配置方式,并且我的测试需要支持它。在测试开始之前,尝试调用一个方法来检查它们是否存在,但找不到任何有效的方法。

我检查了跳板,但两个图标的定义相同。尝试检查

Bundle.allBundles
但他们没有显示沙箱后面的应用程序。我的最后一次尝试是启动测试并尝试在失败时捕获它。

求助于ChatGPT;它声称,如果我换行

XCTContext
,失败将不会擦除测试,但从我的尝试来看,这似乎是虚假的,即使
continueAfterFailure
为真。它还建议类似下面的代码,但启动也不会抛出,所以这甚至不会编译...

    continueAfterFailure = true
    
    var ipaIsNotPresent = false
    let ipaBuild = XCTContext.runActivity(named: "Checking for IPA") { _ in
        do {
            setCurrentApp(.ipaVersionBundleId)
            try app.launch()
        } catch {
            ipaIsNotPresent = true
        }
    }
    
    if ipaIsNotPresent {
        var testFlightIsNotPresent = false
        let testflightBuild = XCTContext.runActivity(named: "Checking for TestFlight version") { _ in
            do {
                setCurrentApp(.testFlightBundleId)
                try app.launch()
            } catch {
                testFlightIsNotPresent = true
            }
        }
        
        if testFlightIsNotPresent {
            isAppInstalledAtAll = false
        } else {
            isAppTestFlightVersion = true
        }
    }

也尝试过这个,但我认为如果从自己项目中的测试运行器运行,bundle将始终为零:

    if Bundle(identifier: BundleId.ipaVersionBundleId.rawValue) == nil {
        if Bundle(identifier: BundleId. testFlightBundleId.rawValue) == nil {
            isAppInstalledAtAll = false
        } else {
            isRunningAgainstTestFlightBuild = true
        }
    } else {
        isRunningAgainstTestFlightBuild = false
    }

如果有人可以建议一种更简单/更好的方法来检测正在测试的捆绑包,我很乐意这样做。否则,是否可以处理启动失败以便我可以使用该信息?

如果需要,我可以将其移至其自己的测试中,而不是设置函数中,然后我认为我可以忽略该测试的失败。我也可以在应用程序中进行更改,但到那时我认为为时已晚。

再次澄清,我的问题是如何在运行测试之前检测应用程序是否存在?我正在尝试的代码的任何替代方案都是完全可以接受的答案。

ios swift config ui-automation xctest
1个回答
0
投票

好吧,在尝试了几种不同的方法(包括将 isStrict 选项设置为 false 时的预期失败)后,结果证明这是微不足道的:

let firstId = "com.blah.blahBlah"
let secondId = "com.blah.internal.blahBlah"

var app = XCUIApplication(bundleIdentifier: firstId)
isAppTestFlightVersion = app.exists

app = XCUIApplication(bundleIdentifier: secondId)
isAppInstalledAtAll = isAppTestFlightVersion || app.exists

if !isAppInstalledAtAll {
    isAppTestFlightVersion = true
}
© www.soinside.com 2019 - 2024. All rights reserved.