有没有办法在 Swift XCTest UI 中的测试之间重置应用程序? [英] Is there a way to reset the app between tests in Swift XCTest UI?

查看:31
本文介绍了有没有办法在 Swift XCTest UI 中的测试之间重置应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 XCTest 中是否有一个 API 调用,我可以将它放入 setUP() 或 tearDown() 以在测试之间重置应用程序?我查看了 XCUIApplication 的点语法,我看到的只是 .launch()

Is there an API call within XCTest that I can put into the setUP() or tearDown() to reset the app between tests? I looked in the dot syntax of XCUIApplication and all I saw was the .launch()

或者有没有办法在 Swift 中调用 shell 脚本?然后我可以在测试方法之间调用 xcrun 来重置模拟器.

OR is there a way to call a shell script in Swift? I could then call xcrun in-between test methods to reset the simulator.

推荐答案

您可以在测试目标中添加一个运行脚本"阶段来构建阶段,以便在对其运行单元测试之前卸载应用程序,不幸的是,这是但不是在测试用例之间.

You can add a "Run Script" phase to build phases in your test target to uninstall the app before running unit tests against it, unfortunately this is not between test cases, though.

/usr/bin/xcrun simctl 卸载启动的 com.mycompany.bundleId

更新

在测试之间,您可以在拆卸阶段通过跳板删除应用.虽然,这确实需要使用来自 XCTest 的私有标头.(标题转储可从 Facebook 的 WebDriverAgent 此处获得.)

Between tests, you can delete the app via the Springboard in the tearDown phase. Although, this does require use of a private header from XCTest. (Header dump is available from Facebook's WebDriverAgent here.)

以下是 Springboard 类中的一些示例代码,用于通过点击并按住从 Springboard 删除应用程序:

Here is some sample code from a Springboard class to delete an app from Springboard via tap and hold:

import XCTest

class Springboard {

    static let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")

    /**
     Terminate and delete the app via springboard
     */
    class func deleteMyApp() {
        XCUIApplication().terminate()

         // Force delete the app from the springboard
        let icon = springboard.icons["Citizen"]
        if icon.exists {
            let iconFrame = icon.frame
            let springboardFrame = springboard.frame
            icon.press(forDuration: 1.3)

            // Tap the little "X" button at approximately where it is. The X is not exposed directly
            springboard.coordinate(withNormalizedOffset: CGVector(dx: (iconFrame.minX + 3) / springboardFrame.maxX, dy: (iconFrame.minY + 3) / springboardFrame.maxY)).tap()

            springboard.alerts.buttons["Delete"].tap()
        }
    }
 }

斯威夫特 3-:

import XCTest

class Springboard {

    static let springboard = XCUIApplication(privateWithPath: nil, bundleID: "com.apple.springboard")

    /**
     Terminate and delete the app via springboard
     */
    class func deleteMyApp() {
        XCUIApplication().terminate()

        // Resolve the query for the springboard rather than launching it
        springboard.resolve()

        // Force delete the app from the springboard
        let icon = springboard.icons["MyAppName"]
        if icon.exists {
            let iconFrame = icon.frame
            let springboardFrame = springboard.frame
            icon.pressForDuration(1.3)

            // Tap the little "X" button at approximately where it is. The X is not exposed directly
            springboard.coordinateWithNormalizedOffset(CGVectorMake((iconFrame.minX + 3) / springboardFrame.maxX, (iconFrame.minY + 3) / springboardFrame.maxY)).tap()

            springboard.alerts.buttons["Delete"].tap()
        }
    }
 }

然后:

override func tearDown() {
    Springboard.deleteMyApp()
    super.tearDown()
}

私有头文件是在 Swift 桥接头文件中导入的.您需要导入:

The private headers were imported in the Swift bridging header. You'll need to import:

// Private headers from XCTest
#import "XCUIApplication.h"
#import "XCUIElement.h"

注意:从 Xcode 10 开始,XCUIApplication(bundleIdentifier:) 现在已被 Apple 公开,并且不再需要私有标头.

Note: As of Xcode 10, XCUIApplication(bundleIdentifier:) is now exposed by Apple, and the private headers are no longer needed.

这篇关于有没有办法在 Swift XCTest UI 中的测试之间重置应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆