Search code examples
iosswiftxcodexcuitestuitest

XCUITest - Open iOS settings and change date/time


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!


Solution

  • 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
    }()