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

标签 xcode xctest ios13 xcode11 xcuitest

最近我开始使用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/

    相关文章:

    swift - 如何解决: 'keyWindow' was deprecated in iOS 13. 0

    ios - Xcode 11 swift 包管理器无法解析包

    ios - 向上滑动和向右滑动在 XCUITest 上无法正常工作 - M1 Mac

    ios - Xcode Playground 运行时间太长

    swift - 在 Xcode UI 测试中编辑记录的元素

    ios - Swift UITest 在 tableviewcell 中找不到 collectionview

    swift - UIScreenEdgePanGestureRecognizer 无法在 iOS 13 模拟器中工作

    swift - 编译器错误: Invalid library file - CoreLocation

    ios - Ionic CLI 构建错误

    c++ - cmake - 显示项目中的其他类文件