gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 02:42:47 29 4
gpt4 key购买 nike

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

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

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

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

    最佳答案

    iOS 14iOS 14 的工作解决方案

    import XCTest

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

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

    let bundleDisplayName = "MyApp"

    let icon = springboard.icons[bundleDisplayName]
    if icon.exists {
    icon.press(forDuration: 1)

    let buttonRemoveApp = springboard.buttons["Remove App"]
    if buttonRemoveApp.waitForExistence(timeout: 5) {
    buttonRemoveApp.tap()
    } else {
    XCTFail("Button \"Remove App\" not found")
    }

    let buttonDeleteApp = springboard.alerts.buttons["Delete App"]
    if buttonDeleteApp.waitForExistence(timeout: 5) {
    buttonDeleteApp.tap()
    }
    else {
    XCTFail("Button \"Delete App\" not found")
    }

    let buttonDelete = springboard.alerts.buttons["Delete"]
    if buttonDelete.waitForExistence(timeout: 5) {
    buttonDelete.tap()
    }
    else {
    XCTFail("Button \"Delete\" not found")
    }
    }
    }

    class HomeUITests: XCTestCase {
    override func setUpWithError() throws {
    // Put setup code here. This method is called before the invocation of each test method in the class.

    // In UI tests it is usually best to stop immediately when a failure occurs.
    continueAfterFailure = false

    // In UI tests it’s 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.
    }

    override func tearDownWithError() throws {
    // Put teardown code here. This method is called after the invocation of each test method in the class.
    }

    func testExample() throws {
    deleteMyApp()

    // UI tests must launch the application that they test.
    let app = XCUIApplication()
    app.launch()

    // Use recording to get started writing UI tests.
    // Use XCTAssert and related functions to verify your tests produce the correct results.
    }

    func testLaunchPerformance() throws {
    if #available(macOS 10.15, iOS 13.0, tvOS 13.0, *) {
    // This measures how long it takes to launch your application.
    measure(metrics: [XCTApplicationLaunchMetric()]) {
    XCUIApplication().launch()
    }
    }
    }
    }

    关于xcode - 如何使用 XCTest 从 iOS 13 删除/重置应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58509372/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com