UI Testing Failure - Did not receive view did disappear notification within 2.0s

Hi folks,


I'm getting a testing error when running my UI tests and can't figure out how to get around it.


Background: we are using a UIAlertController as a loading indicator while a network request occurs. There are no actions associated with this UIAlertController as it is closed automatically when the network activity is completed. We display this alert after the user taps the login button for our app.


When we run our tests, they fail at the point right after this with:


UI Testing Failure - Did not receive view did disappear notification within 2.0s


Per Google, I've tried to use addUIInterruptionMonitor to handle the alert, but without any success. Since there's no action that can be taken on the alert, the interruption monitor just looks like this:


addUIInterruptionMonitor(withDescription: "Loading") { handler in
    return true
}


But...no dice. I get the same error.


What am I doing wrong?


Thanks,

Dan

Accepted Reply

By returning true you are saying you handled the Alert. If you actually need to wait until the alert is gone, then add an expectation to the handler like this one:



let textOnAlert = app.staticTexts["No network, wait."]
let doesNotExist = NSPredicate(format: "exists == false")
let expect = testCase.expectation(for: doesNotExist, evaluatedWith: textOnAlert, handler: nil)
wait(for: [expect], timeout: 10)

Replies

By returning true you are saying you handled the Alert. If you actually need to wait until the alert is gone, then add an expectation to the handler like this one:



let textOnAlert = app.staticTexts["No network, wait."]
let doesNotExist = NSPredicate(format: "exists == false")
let expect = testCase.expectation(for: doesNotExist, evaluatedWith: textOnAlert, handler: nil)
wait(for: [expect], timeout: 10)

Remember that addUIInterruptionMonitor is for System alerts only, if you added a AlertController manually, then you interect with it normally using app.buttons

Ding ding ding! @danielcarlosce, you got it. DTS got back to me with your exact suggestion, I should have updated the post here but yours is the correct answer for why my approach didn't work. Thanks!