首页 > 解决方案 > 如何在 XCUITest 中访问 iOS 14 时间选择器

问题描述

我想使用inline最近在 iOS 14 中引入的日期选择器样式。但是,我需要对几个 XCUITests 进行调整,其中包括以前的选择器轮子样式。

问题是我根本无法更改时间选择器的值。我注意到时间选择器不是XCUITest 中picker的元素。pickerWheel相反,它是一个textField元素。

例如 TextField, {{178.3, 401.7}, {74.7, 36.3}}, label: 'Time', value: 12:01

像典型的文本字段一样更改其值根本不起作用(typeText不做任何事情)。我试图访问似乎在文本字段内的选择器轮,但检查它的后代返回空。

po timeInput.descendants(matching: .any).count
t =   799.62s Get number of matches for: Descendants matching type Any
0 // no descendants found

那么如何更改 XCUITest 中时间选择器文本字段的值呢?

编辑:

的日期选择器模式UIDatePicker设置为time,所以我并没有真正看到日历视图,只是时间输入字段。

我将日期选择器放在contentViewUITableViewCell 的内部,然后在点击另一个表格视图单元格时添加它(即它是动态添加的)。

日期选择器配置如下:

picker.datePickerMode = .time
picker.date = Date(timeInterval: time, since: date)
picker.minuteInterval = intervals
if #available(iOS 14, *) {
    picker.preferredDatePickerStyle = .inline
}

以前,日期选择器显示为选择器轮,我在 XCUITest 中访问它没有问题。我可以简单地调用它来调整选择轮的值: pickerWheels.element(boundBy: index).adjust(toPickerWheelValue: value)

但是,将日期选择器设置为inline时,之前的查询不再有效。我还检查了pickerdatePicker元素,但它们也返回空结果。我可以看到一个带有“时间”标签的文本字段元素,并且该值包含时间选择器中设置的任何值。

(lldb) po app.pickers.count
t =  2270.22s Get number of matches for: Descendants matching type Picker
0 // No results

(lldb) po app.pickerWheels.count
t =  2277.07s Get number of matches for: Descendants matching type PickerWheel
0 // No results

(lldb) po app.datePickers.count
t =  2286.58s Get number of matches for: Descendants matching type DatePicker
0 // No results

(lldb) po app.textFields.count
t =  2302.55s Get number of matches for: Descendants matching type TextField
1 // 1 element matched

(lldb) po app.textFields
t =  2308.08s Requesting snapshot of accessibility hierarchy for app with pid 55829
t =  2308.46s Find: Descendants matching type TextField
t =  2308.47s Requesting snapshot of accessibility hierarchy for app with pid 55829
t =  2308.61s Find: Descendants matching type TextField
t =  2308.61s Requesting snapshot of accessibility hierarchy for app with pid 55829
Find: Target Application
  Output: {
    Application, pid: 55829, label: 'Projects'
  }
  ↪︎Find: Descendants matching type TextField
    Output: {
      TextField, {{178.3, 401.7}, {74.7, 36.3}}, label: 'Time', value: 01:00
    }

所以我看不到任何选择器,但我有文本字段,其值设置为日期选择器的时间输入值。我尝试通过使用更改文本字段的值,typeText但它似乎根本没有做任何事情。

标签: iosuidatepickerios14xcuitest

解决方案


我最终创建了一个仅包含 UIDatePicker 的示例视图控制器,以查看我是否可以在 XCUITest 中访问它。

有趣的是,我能够datePicker在 XCUITest 中检测到一个元素,这是我在表格视图中动态添加 UIDatePicker 时无法做到的。

的后代长datePicker这样:

Find: Descendants matching type DatePicker
Output: {
  DatePicker, {{89.3, 423.3}, {196.7, 53.3}}
}
↪︎Find: Descendants matching type Any
  Output: {
    Other, {{89.3, 423.3}, {196.7, 53.3}}
    Other, {{89.3, 431.3}, {196.7, 37.3}}
    Other, {{89.3, 431.3}, {196.7, 37.3}}, label: 'Time Picker'
    TextField, {{97.3, 431.3}, {74.7, 36.3}}, label: 'Time', value: 05:54
    SegmentedControl, {{178.0, 431.3}, {100.0, 36.3}}
    Button, {{178.0, 431.3}, {49.0, 36.3}}, label: 'AM'
    Button, {{228.0, 431.3}, {50.0, 36.3}}, label: 'PM', Selected
  }

时间输入字段仍然是一个文本字段元素,但我能够使用typeText. 我怀疑我们的代码库中有一些东西可以处理valueChanged导致typeText无法工作的委托。

底线,typeText用于更改时间输入选择器的值应该可以正常工作。


推荐阅读