Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UI Testing - wait for dialog then tap Ok button and check controller

I am trying to create UI test for my controller. I fill textfields and tap button and now I want to wait for dialog to appear and when it apears I want to tap Ok button and check what controller is shown.

This is end of my test method:

let alert = app.alerts["Error"]
let exists = NSPredicate(format: "exists == 1")
self.expectationForPredicate(exists, evaluatedWithObject: alert, handler: nil)
self.waitForExpectationsWithTimeout(5.0) { (error) in

  XCTAssertNil(error, "Something went horribly wrong")
  alert.buttons["Ok"].tap()
  XCTAssertEqual(app.navigationBars.element.identifier, "RegistrationViewController")
}

The problem is that test is evaluated as failure but when I look at phone the dialog appears and Ok button is tapped and controller is also fine.

I get this failure in debug window:

UI Testing Failure - No matches found for Alert

I guess there is somehow problem with that Tap. How can I fix it? What I am doing wrong? Thanks

like image 559
Libor Zapletal Avatar asked Mar 30 '16 16:03

Libor Zapletal


1 Answers

waitforExpectationsWithTimeout() only calls the completion handler when the timeout is exceeded. Simply move your controller assertion beneath the asynchronous wait call.

let alert = app.alerts["Error"]
let exists = NSPredicate(format: "exists == 1")

expectation(for: exists, evaluatedWith: alert, handler: nil)
waitForExpectations(timeout: timeout, handler: nil)

alert.buttons["Ok"].tap()
XCTAssertEqual(app.navigationBars.element.identifier, "RegistrationViewController")
like image 108
Joe Masilotti Avatar answered Nov 04 '22 21:11

Joe Masilotti