XCUITests — Set Date Time or Select value from Picker view

Nishith Shah
Quality Engineering University
2 min readApr 23, 2021

We have two different kind of picker controls — UIPickerView and UIDatePicker. UIPickerView is used for any non date related fields and UIDatePicker is strictly used when we are dealing with date/time.

Photo by Djim Loic on Unsplash

How to select value from UIPickerView?

I want to select “Santa Clara”.

UIPickerView

Following is the script to select the city value as “Santa Clara”.

let pickerWheels = app.pickers["CityPicker"].pickerWheelspickerWheels.element(boundBy: 0).adjust(toPickerWheelValue: "Santa Clara")

How to select value from UIDatePicker?

I want to set 21 January 1990. (My Birthdate 😊)

UIDatePicker

Following is the script to select the city value as 21 January 1990.

let pickerWheels = app.datePickers["BirthdatePicker"].pickerWheelspickerWheels.element(boundBy: 0).adjust(toPickerWheelValue: "January")pickerWheels.element(boundBy: 1).adjust(toPickerWheelValue: "21")pickerWheels.element(boundBy: 2).adjust(toPickerWheelValue: "1990")

Sometimes, it might be possible that the value which we can see in UI may not be similar to select value. To know exact value, you have to print the Picker View hierarchy.

pickerWheels.element(boundBy: 0).debugDescription

Self Exercise

Write the script to select time “6:00 AM” on “Wed Jun 13”.

Let me know — along with any questions, comments, or feedback that you might have on https://www.linkedin.com/in/nshthshah.

--

--