我有一个试图删除应用程序的类teardown,但它无法识别app.terminate()。

class DeviceSettingsUtilities : UITestUtilities {
func removeApp(productName:String){
        print("in teardown")
        let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")
        XCUIApplication().terminate() // this does nothing
        XCUIApplication(bundleIdentifier: "com.xxx.xxxx").terminate()//this does nothing too, but this works when called as an instance teardown
        sleep(5)
        springboard.activate()
        let icon = springboard.icons.matching(identifier: productName).firstMatch
// icon.exists is false when called as a class teardown
// icon.exists is true when called as an instance teardown
        if icon.exists {
            let iconFrame = icon.frame
            let springboardFrame = springboard.frame
            icon.press(forDuration:1.3)
            springboard.coordinate(withNormalizedOffset: CGVector(dx: ((iconFrame.minX + 3) / springboardFrame.maxX), dy:((iconFrame.minY + 3) / springboardFrame.maxY))).tap()
            sleep(5)
            springboard.buttons["Delete"].firstMatch.tap()
            sleep(5)
        }
        XCUIApplication().terminate()
    }

}
这是在测试用例类teardown方法中调用的,如下所示
override class func tearDown() {
    super.tearDown()
    let deviceSettings = DeviceSettingsUtilities()
    deviceSettings.removeApp(productName: ProductName.rawValue)
}

这不会删除应用程序,但是如果我将类func tearDown()更改为func tearDown(),它会毫无问题地删除应用程序。不知道我错过了什么。有什么建议吗?

最佳答案

当您将代码放入类tearDown方法时,应用程序不会被重置,因为该方法只在类中的所有测试完成后运行。实例tearDown是放置每次测试后要运行的代码的最佳位置。
Apple's documentation
对于每个类,测试从运行类设置方法开始。对于每个测试方法,都会分配一个类的新实例并执行其实例设置方法。然后运行测试方法,然后运行实例拆卸方法。这个序列对类中的所有测试方法重复。在类中的最后一个测试方法拆卸完成后,Xcode将执行类拆卸方法并移到下一个类。此序列重复,直到所有测试类中的所有测试方法都已运行。

关于swift - XCUITest类拆卸不删除该应用程序。但是,如果它的实例被拆除,则可以工作。我究竟做错了什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53181823/

10-13 08:30