Delay/Wait in a test case of Xcode UI testing Delay/Wait in a test case of Xcode UI testing ios ios

Delay/Wait in a test case of Xcode UI testing


Additionally, you can just sleep:

sleep(10)

Since the UITests run in another process, this works. I don’t know how advisable it is, but it works.


Asynchronous UI Testing was introduced in Xcode 7 Beta 4. To wait for a label with the text "Hello, world!" to appear you can do the following:

let app = XCUIApplication()app.launch()let label = app.staticTexts["Hello, world!"]let exists = NSPredicate(format: "exists == 1")expectationForPredicate(exists, evaluatedWithObject: label, handler: nil)waitForExpectationsWithTimeout(5, handler: nil)

More details about UI Testing can be found on my blog.


iOS 11 / Xcode 9

<#yourElement#>.waitForExistence(timeout: 5)

This is a great replacement for all the custom implementations on this site!

Be sure to have a look at my answer here: https://stackoverflow.com/a/48937714/971329. There I describe an alternative to waiting for requests which will greatly reduce the time your tests are running!