问题
I have a UIPickerView on my app, with a list of values, we can use Colors. The picker has only one wheel, and I manage to select the value by using this code:
let app = XCUIApplication()
app.launch()
app.pickerWheels.element.adjustToPickerWheelValue("Yellow")
But what I want to do is to get all the possible values of this wheel in an Array, so I can use the array values to select in the picker.
Another good solution would be to make the selection by index instead of by key, but I could not find ways of doing that. Someone?
回答1:
I posted this on another question as well. You could get the number of values in the picker as x, then call this function x number of times. And as the value updates the cell the picker is associated with you can grab the value from there to add it to your array.
Let me know if you have some questions...
Workaround:
let pickerWheel = XCUIApplication().pickers.pickerWheels.element
let appWindowHeight = XCUIApplication().windows.elementBoundByIndex(0).frame.height
let pickerWheelCellHeight = GFloat(44)
func advancePickerWheelOneValue() -> Self {
let topScrollPoint = Double(pickerWheel.frame.midY/appWindowHeight)
let bottomScrollPoint = Double((pickerWheel.frame.midY + pickerWheelCellHeight)/appWindowHeight)
let topScreenPoint = XCUIApplication().windows.elementBoundByIndex(0).coordinateWithNormalizedOffset(CGVector(dx: 0.5, dy: topScrollPoint))
let bottomScreenPoint = XCUIApplication().windows.elementBoundByIndex(0).coordinateWithNormalizedOffset(CGVector(dx: 0.5, dy: bottomScrollPoint))
bottomScreenPoint.pressForDuration(0, thenDragToCoordinate: topScreenPoint)
return self
}
You may need to adjust the dx value depending on where the picker is.
.coordinateWithNormalizedOffset(CGVector(dx: 0.5,
But 0.5 works if the picker occupies the width of the screen.
With this method you an move the picker on value at a time. Call it multiple times and you get to the value you want.
Its not ideal but it can help if you want to check certain indexes. Hopefully you would have to use it on a picker with hundreds of values ;)
来源:https://stackoverflow.com/questions/33842801/how-to-get-all-possible-values-of-a-pickerwheels-on-ios-uitest