Reputation: 137
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!
Upvotes: 8
Views: 5966
Reputation: 13013
In the very end, I gave up, since all solutions are flaky, depending on the device's locale and date. Then I thought by myself: why should I even test this whole UI element? If you need to change date somewhere in your flow, you are better off doing something like this:
Button
s, one with a goForward
SFSymbol and one with goBackwards
.#if DEBUG
statementsDatePicker
by a predefined static value. That way, you can still test different dates, but no more flaky tests.Upvotes: 0
Reputation: 3241
For anyone, supporting multiple localizations, an easier way would be
let dateComponent = Calendar.current.dateComponents([.day, .month, .year], from: Date())
let formatter = DateFormatter()
formatter.dateFormat = "MMMM"
let monthText = formatter.string(from: Date())
datePicker.pickerWheels[String(dateComponent.year!)].adjust(toPickerWheelValue: "2017")
datePicker.pickerWheels[monthText].adjust(toPickerWheelValue: "December")
datePicker.pickerWheels[String(dateComponent.day!)].adjust(toPickerWheelValue: "21")
Upvotes: 9
Reputation: 716
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.
Upvotes: 25