How to dismiss a popover in a UI Test
Asked Answered
G

2

6

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'

Glindaglinka answered 11/5, 2017 at 0:53 Comment(0)
S
8

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()
    }
}
Stanleystanly answered 3/10, 2017 at 21:28 Comment(3)
Please review How do I write a good answer. Code-only answers are discouraged because they do not explain how they resolve the issue in the question. You should update your answer to explain what this does and how to use it.Gull
@Gull Better?Matchless
Thanks for updating your answer to explain the code. Your answer had been flagged as low quality, as it was code-only. Its not up to me alone, but it looks good to me and should pass the review process :)Gull
R
2

Add this line where you want to dismiss the popover

app.children(matching: .window).element(boundBy: 0).tap()
Resentful answered 11/5, 2017 at 14:6 Comment(2)
That still does not work for me. It looks like this tries to tap in the center of the screen. The view controller in my popover covers the center of the screen and the result is just that a different row in the table view that is contained in my view controller gets selected.Glindaglinka
Do you have some other elements on screen you can try tapping on those.Resentful

© 2022 - 2024 — McMap. All rights reserved.