4

Is it possible to change a device's date/time settings within a UI test?

I'm able to open the Settings app, naviguate to General > Date & Time and turn off the "Set Automatically" button. However, I can't find a way to change the pickerWheel value.

let settingsApp = XCUIApplication(bundleIdentifier: "com.apple.Preferences")
settingsApp.launch()
settingsApp.tables.cells["General"].tap()
settingsApp.tables.cells["Date & Time"].tap()
settingsApp.switches["Set Automatically"].tap()
// Change pickerWheel value?

Any thoughts? Is it at all possible?

Thanks!

1 Answers1

2

I use the following code to do it:

// Here I format the Time to get the right format
var decomposedDateTime: [String] = Formatters.timeFormatter.string(from: date)
            .replacingOccurrences(of: ":", with: " ")
            .split(separator: " ")
            .map({String($0)})

// Then I format the Day and add it at first        
decomposedDateTime.insert(Formatters.datePickerFormatter.string(from: date), at: 0)

// Update picker wheels accordingly
for i in 0...(decomposedDateTime.count - 1) {
    settingsApp.pickerWheels.element(boundBy: i).adjust(toPickerWheelValue: decomposedDateTime[i])
}

Here are my formatters

static let timeFormatter: DateFormatter = {
    let f = DateFormatter()
    f.dateStyle = .none
    f.timeStyle = .short
    return f
}()

static let datePickerFormatter: DateFormatter = {
    let f = DateFormatter()
    f.dateStyle = .medium
    f.dateFormat = "MMM d"
    return f
}()
cesarmarch
  • 617
  • 4
  • 13
  • Thank you so much, that's very helpful! I'm still having an issue where Xcode doesn't seem to find the pickerWheel:`No matches found for Descendants matching type PickerWheel` running iOS 12.3.1 and Xcode 11. Are you able to find that element within the Settings app? – iOSTesterDD Jan 28 '20 at 17:50
  • You have first to disable the `Set Automatically` option and then click on the last cell to display the `PickerWheel`. Then, you should be able to update the date/time – cesarmarch Jan 29 '20 at 14:56
  • 1
    Thank you so much! All I was missing was a tap on the last cell... Got it to work, thanks again!! – iOSTesterDD Jan 29 '20 at 18:42