iosios9xcode-ui-testingxcode7-beta2xctwaiter

Delay/Wait in a test case of Xcode UI testing


I am trying to write a test case using the new UI Testing available in Xcode 7 beta 2. The App has a login screen where it makes a call to the server to login. There is a delay associated with this as it is an asynchronous operation.

Is there a way to cause a delay or wait mechanism in the XCTestCase before proceeding to further steps?

There is no proper documentation available and I went through the Header files of the classes. Was not able to find anything related to this.

Any ideas/suggestions?


Solution

  • 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.