Search code examples
iosswiftxcode-ui-testinguitest

Get success on UITest when waitForExpectations fails


I have a task to get UITest successfully passed, if expected element (alert in my case) DIDN'T appear before timeout (on server request).

Code:

let element = app.alerts["test text"]
let existsPredicate = NSPredicate(format: "exists == true")
expectation(for: existsPredicate, evaluatedWith: element, handler: nil)

waitForExpectations(timeout: 10, handler: nil)

// when timeout expired test should finish with success

Didn't find any solutions over Internet.


Solution

  • Оооо, you should mind it’s UITest. In that case expectation has nice property: isInverted. Setting this to true, says that expectation is not intended to happen. )))

    let element = app.alerts["test text"]
    let existsPredicate = NSPredicate(format: "exists == true")
    let expectation = expectation(for: existsPredicate, evaluatedWith: element, handler: nil)
    expectation.isInverted = true // if alert doesn't appear during 10 seconds, then test passed
    
    waitForExpectations(timeout: 10, handler: nil)