'How to assert on the text in a DatePicker which has the compact style?

I am rendering a DatePicker in my application with the compact style as follows:

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

I am able to tap on the DatePicker in a UI test, select the date that I want in the calendar-style popup, and then dismiss 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()

(All of the above is demonstrated in this sample application.)

Is it possible to assert that the DatePicker is now showing the date that I selected? If so, how?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source