【问题标题】:XCUITest - Open iOS settings and change date/timeXCUITest - 打开 iOS 设置并更改日期/时间
【发布时间】:2020-01-24 23:33:32
【问题描述】:

是否可以在 UI 测试中更改设备的日期/时间设置?

我可以打开“设置”应用,导航到“常规”>“日期和时间”,然后关闭“自动设置”按钮。但是,我找不到更改pickerWheel 值的方法。

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?

有什么想法吗?有可能吗?

谢谢!

【问题讨论】:

    标签: ios swift xcode xcuitest uitest


    【解决方案1】:

    我使用下面的代码来做:

    // 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])
    }
    

    这是我的格式化程序

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

    【讨论】:

    • 非常感谢,这很有帮助!我仍然遇到 Xcode 似乎找不到运行 iOS 12.3.1 和 Xcode 11 的 pickerWheel:No matches found for Descendants matching type PickerWheel 的问题。你能在“设置”应用中找到该元素吗?
    • 您必须首先禁用Set Automatically 选项,然后单击最后一个单元格以显示PickerWheel。然后,您应该能够更新日期/时间
    • 非常感谢!我所缺少的只是点击最后一个单元格......让它工作,再次感谢!
    猜你喜欢
    • 1970-01-01
    • 2021-01-27
    • 2016-04-30
    • 1970-01-01
    • 2016-07-13
    • 2021-03-31
    • 1970-01-01
    • 2018-09-08
    • 1970-01-01
    相关资源
    最近更新 更多