我正在尝试实现一个UI测试,该测试调整示例选择器的滚轮的选定值,如下所示:
import SwiftUI
struct ContentView: View {
var colors = ["Red", "Green", "Blue", "Yellow", "White", "Black", "Purple", "Cyan"]
@State private var selectedColor = "Red"
var body: some View {
VStack {
Picker("Choose a color", selection: $selectedColor) {
ForEach(colors, id: \.self) {
Text($0)
}
}
.accessibility(identifier: "picker")
Text("You selected: \(selectedColor)")
}
}
}
UI测试
let picker = app!.pickers["picker"].pickerWheels.firstMatch
picker.adjust(toPickerWheelValue: "Purple")
但是,当使用函数adjust(toPickerWheelValue:)时,控制盘仅在所需值的方向上跳转到列表中的下一项。在上面的UI测试示例中,结果是滚轮移动到"Green“。如果我再次调用该函数,轮子将移动一步以选择"Blue“,依此类推。为什么会发生这种情况?
使用:Xcode 12.4和iOS 14.4
发布于 2021-02-21 12:05:25
adjust(toPickerWheelValue:)的行为看起来有点违反直觉。您可以编写自己的函数来完成此任务:
extension XCUIElement{
func selectPicker(value: String, timeout: TimeInterval) {
let pickerWheel = pickerWheels.firstMatch
let row = pickerWheels[value]
while !row.waitForExistence(timeout: timeout) {
pickerWheel.adjust(toPickerWheelValue: value)
}
}
}
然后像这样使用它:
let picker = app.pickers["picker"]
picker.selectPicker(value: "Purple", timeout: 1)
https://stackoverflow.com/questions/66297762
复制相似问题