Cannot dismiss date pickers in iOS 11.3.1 in UI automated tests

Hi Guys, i am currently having problems dismissing date pickers on iOS 11.3.1 on the iPad in UI automated tests.

static func dismiss() {
        let app = XCUIApplication()
        let popoverDismissRegionElement = app.otherElements["PopoverDismissRegion"]
        if popoverDismissRegionElement.exists && popoverDismissRegionElement.isHittable {
            popoverDismissRegionElement.tap()
        } else {
            let window = app.windows.element(boundBy: 0)
            window.tap()
        }
    }

I was able to dismis them in UI test's prior to upgrading to iOS 11.3.1.

Can you show the code where you dismiss the picker ?

This is how i dismiss the date picker



if (UIDevice.current.userInterfaceIdiom == .pad) {
            DatePicker.dismiss()
} else {
            NavigationBar.done()
}

How did you declare datePicker instance ?


var myDatePicker: UIDatePickerController!

   myDatePicker.dismissViewControllerAnimated(true, completion: nil)


Why do you dismiss the navigationBar if iPhone ?


Does your code work on iPad ? on iPhone ?

it used to work all this time. This problem arose after i updated iOS to iOS 11.3.1. This code works in previous versions of iOS. It feels like its a apple bug on iOS 11.3.1

Try with the dismiss function on your instance of datePicker


myDatePicker.dismissViewControllerAnimated(true, completion: nil)

I am having the same problem. It seems that when you present a date picker in a popover and that popover occludes the center of the PopoverDismissRegion, it's no longer possible to tap on the PopoverDismissRegion.


Did you ever find a workaround?


(Claude31: NB the issue is with XC UI testing -- dismissViewControllerAnimated() does not apply here.)

I am declaring a DatePicker in a SwiftUI view in my app as follows:

DatePicker("Date Selected", selection: $selectedDate, displayedComponents: [.date])
    .accessibilityIdentifier("DatePicker")
    .datePickerStyle(.compact)

After many wasted hours, I have managed to write a UI test which taps on the DatePicker to reveal the calendar-style popup, chooses the desired date in the popup and then dismisses the popup, as follows:

// 1. Show the DatePicker popup
application.datePickers["DatePicker"].tap()
        
// 2. Choose a date in the popup
application.datePickers.collectionViews.buttons["Friday, January 14"].tap()
        
// 3. Dismiss the DatePicker popup
application.datePickers["DatePicker"].tap()

This is working for me on iOS 15.2 devices.

I have put together a minimal SwiftUI app which demonstrates this here.

Cannot dismiss date pickers in iOS 11.3.1 in UI automated tests
 
 
Q