如何使用 XCTest 从 iOS 13 删除/重置应用程序?

Raf*_* C. 7 xcode xctest xcuitest ios13 xcode11

最近我开始使用 XCTest 测试 iOS 应用程序,但我发现了一些困难,主要困难是删除或重置每个测试类中的应用程序内容。

我目前正在使用 XCode 11 并尝试从 iOS 13 中为每个测试类删除/重置应用程序,我已经尝试过:

  • 通过跳板删除应用程序
  • 转至应用程序设置删除应用程序

这一步在我的测试中非常重要,因为在每个测试中我都需要创建一个配置文件并登录,所以在下一个测试中我需要从头开始安装应用程序

Nik*_*Nik 8

iOS 14

\n

工作解决方案为iOS 14

\n
import XCTest\n\nlet springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")\n\nfunc deleteMyApp() {\n    XCUIApplication().terminate()\n\n    let bundleDisplayName = "MyApp"\n\n    let icon = springboard.icons[bundleDisplayName]\n    if icon.exists {\n        icon.press(forDuration: 1)\n\n        let buttonRemoveApp = springboard.buttons["Remove App"]\n        if buttonRemoveApp.waitForExistence(timeout: 5) {\n            buttonRemoveApp.tap()\n        } else {\n            XCTFail("Button \\"Remove App\\" not found")\n        }\n\n        let buttonDeleteApp = springboard.alerts.buttons["Delete App"]\n        if buttonDeleteApp.waitForExistence(timeout: 5) {\n            buttonDeleteApp.tap()\n        }\n        else {\n            XCTFail("Button \\"Delete App\\" not found")\n        }\n\n        let buttonDelete = springboard.alerts.buttons["Delete"]\n        if buttonDelete.waitForExistence(timeout: 5) {\n            buttonDelete.tap()\n        }\n        else {\n            XCTFail("Button \\"Delete\\" not found")\n        }\n    }\n}\n\nclass HomeUITests: XCTestCase {\n    override func setUpWithError() throws {\n        // Put setup code here. This method is called before the invocation of each test method in the class.\n\n        // In UI tests it is usually best to stop immediately when a failure occurs.\n        continueAfterFailure = false\n\n        // In UI tests it\xe2\x80\x99s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.\n    }\n\n    override func tearDownWithError() throws {\n        // Put teardown code here. This method is called after the invocation of each test method in the class.\n    }\n\n    func testExample() throws {\n        deleteMyApp()\n\n        // UI tests must launch the application that they test.\n        let app = XCUIApplication()\n        app.launch()\n\n        // Use recording to get started writing UI tests.\n        // Use XCTAssert and related functions to verify your tests produce the correct results.\n    }\n\n    func testLaunchPerformance() throws {\n        if #available(macOS 10.15, iOS 13.0, tvOS 13.0, *) {\n            // This measures how long it takes to launch your application.\n            measure(metrics: [XCTApplicationLaunchMetric()]) {\n                XCUIApplication().launch()\n            }\n        }\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n


Rom*_*rov 3

尝试按应用程序图标比以前的 iOS 版本长一点。

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

    func deleteMyApp() {
        XCUIApplication().terminate()

        let icon = springboard.icons["YourAppName"]
        if icon.exists {
            let iconFrame = icon.frame
            let springboardFrame = springboard.frame
            icon.press(forDuration: 5)

            // 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()
        }
    }
Run Code Online (Sandbox Code Playgroud)