Search code examples
iosswiftnspredicateuitest

wait for expectation but continue


What is the best way to wait for an expectation to happen, but continue otherwise? I have tried:

expectation(for: NSPredicate(format: "exists == true"), evaluatedWith: app.buttons["myButton"], handler: nil)
waitForExpectations(timeout: 5) { (error) -> Void in
            if error == nil {
                // Do other stuff and continue
            }
}
//continue

Unfortunatelly, that doesn't work since the test stops if the expectation isn't met


Solution

  • In the case of exists == true, you can use waitForExistence -> which returns a boolean value. Once that boolean value is returned (after your timeout is reached.. or when the element exists) you could use that value to determine whether or not to continue. i.e.

    let myButtonExists = app.buttons["myButton"].waitForExistence(timeout: 5)
    if myButtonExists {
       //do something
    } else {
       //do something else
    }
    

    https://developer.apple.com/documentation/xctest/xcuielement/2879412-waitforexistence