I encountered a problem with Xcode 7 UI Testing.
The app displays two alerts after my user logs in, the Request Location Alert and the Push
class BaseTest: XCTestCase {
let pushSent = NSNotification.Name.init("alert.pushSent")
var notificationMonitor: NSObjectProtocol?
override func setUp() {
listenNotifications()
let app = XCUIApplication()
notificationMonitor = addUIInterruptionMonitor(withDescription: "Push Notifications") { [unowned self] (alert) -> Bool in
let btnAllow = app.buttons["Allow"]
//1:
if btnAllow.exists {
btnAllow.tap()
NotificationCenter.default.post(name: self.pushSent, object: nil)
return true
}
//2:
//takeScreenshot
XCTFail("Unexpected System Alert")
return false
}
//3:
//add code for "Request Location" monitor
app.launchEnvironment = ["UITEST_DISABLE_ANIMATIONS" : "YES"]
//4:
app.launch()
}
func listenNotifications() {
NotificationCenter.default.addObserver(forName: pushSent, object: nil, queue: nil) { (notification) in
if let locationDialogHandeler = self.notificationMonitor {
//5:
self.removeUIInterruptionMonitor(locationDialogHandeler)
}
}
}
}
1: Check if you're in the correct alert, tap the button and find a way to remove the monitor (I'm using NotificationCenter)
2: If you enter a monitor and can not find the right button, it means it's an unexpected flow. Fail the test (but take a screenshot first).
3: Add other monitors
4: I am adding monitor even before launching the app. If you add a monitor after the alert appears, it will not be triggered.
5: Remove the monitor, that way when a new alert appears, the next monitor in the stack will be called.
P.S: You should add monitors in reverse order, therefore, add "Request Location" after "Push Notifications"