How to speed up UI test cases in Xcode?

前端 未结 7 1627
自闭症患者
自闭症患者 2020-12-13 14:29

Since Xcode 7 we have a nice API for UI testing. Mostly I\'m satisfied with it. The only concern is related to the speed.

In the beginning an ordina

相关标签:
7条回答
  • 2020-12-13 15:29

    I decrease my UITests time in 30%, follow all steps:

    When you run your app, add the argument:

    let app = XCUIApplication()
    
    override func setUp() {
        super.setUp()
    
        continueAfterFailure = false
        app.launchArguments += ["--Reset"]
        app.launch()
    }
    

    Now, in your AppDelegate add:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        setStateForUITesting()
    }
    
    static var isUITestingEnabled: Bool {
        get {
            return ProcessInfo.processInfo.arguments.contains("--Reset")
        }
    }
    
    private func setStateForUITesting() {
        if AppDelegate.isUITestingEnabled {
            // If you need reset your app to clear state
            UserDefaults.standard.removePersistentDomain(forName: Bundle.main.bundleIdentifier!)
    
            // To speed up your tests
            UIApplication.shared.keyWindow?.layer.speed = 2
            UIView.setAnimationsEnabled(false)
        }
    }
    
    

    In your code, to verify if is in test mode, you can use:

    if AppDelegate.isUITestingEnabled {
        print("Test Mode")
    }
    
    

    Additionally, to can wait while the element load I created this extension:

    import XCTest
    
    extension XCUIElement {
        func tap(wait: Int, test: XCTestCase) {
            if !isHittable {
                test.expectation(for: NSPredicate(format: "hittable == true"), evaluatedWith: self, handler: nil);
                test.waitForExpectations(timeout: TimeInterval(wait), handler: nil)
            }
            tap()
        }
    }
    

    Use like this:

    app.buttons["start"].tap(wait: 20, test: self)
    
    0 讨论(0)
提交回复
热议问题