简体   繁体   English

如何在 XCUITest 中访问 iOS 14 时间选择器

[英]How to access the iOS 14 time picker in XCUITest

I want to make use of the inline date picker style that was recently introduced in iOS 14. However, I need to make adjustments to a couple of XCUITests that include the previous picker wheel style.我想利用最近在 iOS 14 中引入的inline日期选择器样式。但是,我需要对包括以前的选择器轮样式的几个 XCUITest 进行调整。

The problem is I can't change the value of the time picker at all.问题是我根本无法更改时间选择器的值。 I noticed that the time picker is not a picker nor a pickerWheel element in XCUITest;我注意到时间选择picker不是pickerWheelpicker也不是pickerWheel元素; Instead, it's a textField element.相反,它是一个textField元素。

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

Changing its value like a typical textfield doesn't work at all ( typeText doesn't do anything).像典型的文本字段一样更改其值根本不起作用( typeText不做任何事情)。 I tried to access the picker wheels that seem to be inside the textfield, but checking for its descendants returns empty.我试图访问似乎在文本字段内的选择器轮子,但检查其后代返回空。

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

So how do I change the value of the time picker text field in XCUITest?那么如何更改 XCUITest 中时间选择器文本字段的值呢?

EDIT:编辑:

The date picker mode for the UIDatePicker is set to time , so I'm not really seeing the calendar view, just the time input field. UIDatePicker的日期选择器模式设置为time ,所以我没有真正看到日历视图,只是时间输入字段。

I put the date picker inside the contentView of a UITableViewCell, which is then added when another table view cell is tapped (ie it's dynamically added).我将日期选择器放在 UITableViewCell 的contentView中,然后在点击另一个表格视图单元格时添加它(即它是动态添加的)。

The date picker is configured like this:日期选择器配置如下:

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

Previously, the date picker is displayed as a picker wheel and I had no problem accessing it in the XCUITest.以前,日期选择器显示为一个选择器轮,我在 XCUITest 中访问它没有问题。 I could simply call this to adjust the value of the picker wheel: pickerWheels.element(boundBy: index).adjust(toPickerWheelValue: value)我可以简单地调用它来调整选择轮的值: pickerWheels.element(boundBy: index).adjust(toPickerWheelValue: value)

However, with the date picker set to inline , the previous query no longer works.但是,将日期选择器设置为inline ,之前的查询不再有效。 I also checked for picker and datePicker elements, but they also return empty results.我还检查了pickerdatePicker的元素,但他们也返回空的结果。 I can see a textfield element that has a "Time" label and the value contains whatever value is set in the time picker.我可以看到一个带有“时间”标签的文本字段元素,该值包含时间选择器中设置的任何值。

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

So I can't see any pickers, but I have textfield whose value is set to the date picker's time input value.所以我看不到任何选择器,但我有文本字段,其值设置为日期选择器的时间输入值。 I tried changing the textfield's value by using typeText but it doesn't seem to do anything at all.我尝试使用typeText更改文本字段的值,但它似乎根本没有做任何事情。

I ended up creating a sample view controller which contains just the UIDatePicker to see if I can access it in XCUITest.我最终创建了一个仅包含 UIDatePicker 的示例视图控制器,以查看我是否可以在 XCUITest 中访问它。

Interestingly, I was able to detect a datePicker element in the XCUITest which is something I wasn't able to do when dynamically adding the UIDatePicker in a table view.有趣的是,我能够在datePicker中检测到datePicker元素,这是我在表视图中动态添加 UIDatePicker 时无法做到的。

The descendants of the datePicker looks like this: 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
  }

The time input field is still a textfield element but I was able to change its value using typeText .时间输入字段仍然是一个文本字段元素,但我能够使用typeText更改其值。 I suspect that there's something in our codebase that handles the valueChanged delegate that causes the typeText to not work.我怀疑我们的代码库中有一些东西处理了导致typeTextvalueChanged委托。

Bottomline, using typeText to change the value of the time input picker should work just fine.底线,使用typeText更改时间输入选择器的值应该可以正常工作。

Before using typeText to change its value, make sure it has focus.在使用typeText更改其值之前,请确保它具有焦点。 Try to tap the textfield first, and then the value can be changed via typeText .尝试先tap文本字段,然后可以通过typeText更改值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM