Interacting with macOS SwiftUI Graphical DatePicker in UI Tests

I am currently working on a SwiftUI project and I am trying to write a UI test for a DatePicker. However, I am having trouble interacting with the DatePicker in the test. Here is the code for the DatePicker in my SwiftUI view:

DatePicker("Date", selection: $date, displayedComponents: [.date])
    .datePickerStyle(.graphical)
    .labelsHidden()
    .frame(width: 150)

And here is the code for my UI test:

func testSelectingDateChangesMainView() throws {
    let app = XCUIApplication()
    app.launch()
    let dateDatePicker: XCUIElement = app.windows["mainUI-AppWindow-1"].datePickers["Date"]
    dateDatePicker.click()
    let col = dateDatePicker.descendants(matching: .any)
    print("\(dateDatePicker.debugDescription), \(col.debugDescription)")
}

When I run the test, it seems like the DatePicker does not have any descendants, so I am unable to select a date.

Does anyone have any suggestions on how I can interact with a .graphical style DatePicker in a UI test? Any help would be greatly appreciated.

Thank you, OnlyForF1

Post not yet marked as solved Up vote post of OnlyForF1 Down vote post of OnlyForF1
439 views
  • Can a moderator please remove or edit this post to remove the private information about my employer in the final line cheers.

  • Done.

Add a Comment

Replies

Maybe the DatePicker doesn't have any accessible children. Have you checked Accessibility Inspector to see if this DatePicker has any accessibility elements as children?