Search code examples
xcodexcode-ui-testing

How to dismiss a system dialog during a Xcode UITest


In my unit tests there is a page that asks permisson for library usage. While my unit test running, this permisson dialog appears on screen and does not dissappear even if all of my unit tests finish. When UI Tests try to run, they can't cause of this dialog. Is there any way to run UI Tests before Unit Tests ?


Solution

  • So your real problem is to get rid of the system dialog when running UITests. Running UITests before UnitTests won't change a thing, because then the system dialog would pop up during the UITest.

    You can dismiss the dialog like this (in your UITest):

    addUIInterruptionMonitor(withDescription: "“RemoteNotification” Would Like to Send You Notifications") { (alerts) -> Bool in
       if(alerts.buttons["Allow"].exists){
          alerts.buttons["Allow"].tap();
       }
       return true;
    }
    XCUIApplication().tap()
    

    You have to change the description because the above code dismisses a system alert that asks for permission to send push notifications.

    It is important that this code comes BEFORE your test triggers the system dialog. You can put it inside your test function right after you launch the app and before the test does anything else.