Charlie Seligman Charlie Seligman - 3 months ago 20
iOS Question

Xcodes UI Testing showing out of date page info

Currently have the following method:

func waitForOptionalElement(explicitElement: XCUIElement, timeout: Int) {
var waitedDuration = 0
while waitedDuration < timeout {
if explicitElement.exists {
break
}
waitedDuration++
sleep(1)
}
}


This method is aimed at waiting until an object appears on the screen. The trouble I have is that if explicityElement is not present on the screen when the method is called, then explicitElement.exists always returns a false (even when the object appears). It is like the explicitElement is not refreshed when the page changes, but keeps checking the original view rather than any updated view.

If I call the explicitElement in full e.g. XCUIApplication().staticTexts["Error message"].exists then that will come back true. Its as if you need to call XCUIApplication()... to get an updated view of the current page?

Anyone know any clever ways round this?

Answer

It may be more appropriate to use expectationForPredicate.

expectationForPredicate(NSPredicate(format: "exists == true"), evaluatedWithObject: XCUIApplication().staticTexts["Error message"], handler: nil)
waitForExpectationsWithTimeout(15.0, handler: nil)