Search code examples
macosxcode-ui-testing

How to set value of date picker in macOS?


The recorder generated the following code, but click() always clicks on the year field. I did click on the individual fields when recording.

    let startDateDatePicker = sheetsQuery.datePickers["Start Date"]
    startDateDatePicker.click()
    startDateDatePicker.typeText("1")
    startDateDatePicker.click()
    startDateDatePicker.typeText("1")
    startDateDatePicker.click()
    startDateDatePicker.typeText("2016")

What is the recommended way to set a date picker UI element?


Solution

  • startDateDatePicker.dateValue = foo?

    EDIT: Now that I know what you're actually trying to do, try this:

    datePicker.coordinate(withNormalizedOffset: CGVector(dx: 0.1, dy: 0.5)).click()
    datePicker.typeText("1")
    datePicker.coordinate(withNormalizedOffset: CGVector(dx: 0.3, dy: 0.5)).click()
    datePicker.typeText("2")
    datePicker.coordinate(withNormalizedOffset: CGVector(dx: 0.6, dy: 0.5)).click()
    datePicker.typeText("1934")