5

Is there a way to wait for all network requests to finish when testing UI in XCode?

I have an app that sends HTTP requests to get some data from a server, and, in UI tests, I'd like to wait for this data to be retrieved before continuing. Currently I'm using sleep(1) but this approach doesn't seem reliable.

Jeiwan
  • 954
  • 6
  • 13

2 Answers2

8

Your best bet is to wait for some UI element to appear or disappear. Think of it this way:

The framework acts like a user. It doesn't care what code is running under the hood. The only thing that matters is what is visible on the screen.

That said, here is how you can wait for a label titled "Go!" to appear in your UI Tests.

let app = XCUIApplication()
let goLabel = self.app.staticTexts["Go!"]
XCTAssertFalse(goLabel.exists)

let exists = NSPredicate(format: "exists == true")
expectationForPredicate(exists, evaluatedWithObject: goLabel, handler: nil)

app.buttons["Ready, set..."].tap()
waitForExpectationsWithTimeout(5, handler: nil)
XCTAssert(goLabel.exists)

You could also extract that into a helper method. If you use some Swift compiler magic you can even get the failure message to occur on the line that called the method.

private fund waitForElementToAppear(element: XCUIElement, file: String = #file, line: UInt = #line) {
    let existsPredicate = NSPredicate(format: "exists == true")
    expectationForPredicate(existsPredicate, evaluatedWithObject: element, handler: nil)

    waitForExpectationsWithTimeout(5) { (error) -> Void in
        if (error != nil) {
            let message = "Failed to find \(element) after 5 seconds."
            self.recordFailureWithDescription(message, inFile: file, atLine: line, expected: true)
        }
    }
}
Joe Masilotti
  • 16,815
  • 6
  • 77
  • 87
2

You can set up your methods with delegates or completion blocks, and in your test cases use a XCTestExpectation which you can fulfill when the data has been returned.

pbush25
  • 5,228
  • 2
  • 26
  • 35