Wait for all HTTP requests to finish in XCode UI tests?
Asked Answered
K

2

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.

Kyd answered 31/3, 2016 at 2:21 Comment(0)
K
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)
        }
    }
}
Kerstin answered 31/3, 2016 at 11:14 Comment(0)
T
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.

Tweeze answered 31/3, 2016 at 4:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.