Xcode 7 UI Testing: Dismiss Push and Location alerts

后端 未结 4 1766
生来不讨喜
生来不讨喜 2020-12-09 08:26

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

4条回答
  •  情话喂你
    2020-12-09 08:28

    While not ideal, I found that if you simply wait until one authorization dialog has finished before presenting another one in the app, UI tests can pick up multiple requests in a row.

        if CLLocationManager.authorizationStatus() == .AuthorizedWhenInUse || CLLocationManager.authorizationStatus() == .AuthorizedAlways {
            self.locationManager.requestLocation()
        } else {
            self.contactStore.requestAccessForEntityType(.Contacts) { _ in
                self.locationManager.requestWhenInUseAuthorization()
            }
        }
    

    I'm actually requesting access to contacts in a different place in my code, but it can handle multiple simultaneous requests just fine.

    Then in my test:

        addUIInterruptionMonitorWithDescription("Location Dialog") { (alert) -> Bool in
            let button = alert.buttons["Allow"]
            if button.exists {
                button.tap()
                return true
            }
            return false
        }
        addUIInterruptionMonitorWithDescription("Contacts Dialog") { (alert) -> Bool in
            let button = alert.buttons["OK"]
            if button.exists {
                button.tap()
                return true
            }
            return false
        }
    
        app.buttons["Location"].tap()
    
        app.tap() // need to interact with the app for the handler to fire
        app.tap() // need to interact with the app for the handler to fire
    

提交回复
热议问题