Search code examples
iosswiftdatepickeruitest

iOS UI Test Datepicker


I'm making a UI Test for an iOS app. I'm having trouble finding out how to use automated testing for a date picker. Right now I'm stuck trying to find the date picker itself so I can change the values.

I've searched many threads regarding this issue but have found nothing.

If anyone has any insight to this it would be much appreciated. Thanks!


Solution

  • To get started, I'd advise you to use the "Record UI Test" button in Xcode to easily find out how to access the UIDatePicker in the UI Test.

    For a simple UIDatePicker in date mode, changing the date is as simple as:

    let datePickers = XCUIApplication().datePickers
    datePickers.pickerWheels.element(boundBy: 0).adjust(toPickerWheelValue: "June")
    datePickers.pickerWheels.element(boundBy: 1).adjust(toPickerWheelValue: "1")
    datePickers.pickerWheels.element(boundBy: 2).adjust(toPickerWheelValue: "2015")
    

    If the datePicker is connected to a UILabel, you could then check if the text in the label has been updated correctly.