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

2

6

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

Thais answered 30/3, 2016 at 16:26 Comment(0)
S
8

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")
Schinica answered 30/3, 2016 at 16:41 Comment(2)
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.Thais
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 :)Thais
T
0

The alert dialog is a system alert, so it's not accessible via app.alerts.

To access the system alerts, you can use the SpringBoard application like this:

 let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")

The system alerts are available in the SpringBoard application. You can now access the dialog using this approach.

springboard.buttons["Ok"]
Toplevel answered 27/8 at 5:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.