Search code examples
xcodexctestios13xcode11xcuitest

How to delete/reset an app from iOS 13 with XCTest?


Recently I started testing an iOS app using XCTest but I found some difficulties, the main difficulty was deleting or resetting the app content in each test class.

I'm currently using XCode 11 and trying to delete/reset an app from iOS 13 for each test class, I've already tried:

  • Delete app through springboard
  • Delete app by going to the app settings

This step is really important in my tests because in each test I need to create a profile and log in, so in the next test I need to have the app just installed from scratch


Solution

  • Try to press the app icon a little longer than in previous iOS versions.

        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()
            }
        }