5

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

Libor Zapletal
  • 13,752
  • 20
  • 95
  • 182

1 Answers1

7

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")
Valentin Shamardin
  • 3,569
  • 4
  • 34
  • 51
Joe Masilotti
  • 16,815
  • 6
  • 77
  • 87
  • This is what I've tried before but it has same result for me. Still at the end I get same failure that alert dialog is not found. – Libor Zapletal Mar 30 '16 at 16:59
  • Thanks for help. Your solution works. The problem was that confirm button in dialog was OK but in my test I have Ok. I don't know that accessibility identifier is case sensitive but now I don't forget it :) – Libor Zapletal Mar 31 '16 at 04:31