How to dismiss a popover in a UI Test

ios, swift3, ui-testing, uitest

Solution

There's a bug in Xcode UI Tests recording.

Popover dismissal is recorded as:

app.otherElements["PopoverDismissRegion"].tap()

In reality the following is needed (as revealed by Accessibility Inspector):

app.otherElements["dismiss popup"].tap()

Handy extension:

extension XCUIApplication {
    func dismissPopup() {
        otherElements["dismiss popup"].tap()
    }
}

Problem

I have a view controller that is presented using a "Present As Popover" segue. When I run the app it works as expected and tapping outside of the popover will dismiss it. However, when I run my UI test, I can't get the popover to dismiss. How should I do this? I've tried: ``` app.otherElements["PopoverDismissRegion"].tap() ``` But the logs print: Unable to find hit point for Other 0x61000017f8c0: traits: 35184372088832, {{0.0, 0.0}, {375.0, 667.0}}, identifier: 'PopoverDismissRegion', label: 'dismiss popup'

Original source