问题
I've hit a roadblock in trying to write some automated tests for my iPhone app. Judging from the documentation I feel like this should select the first row of the first component of my UIPickerView:
var picker = UIATarget.localTarget().frontMostApp().mainWindow().pickers()[0];
var aWheel = picker.wheels()[0];
var someVals = aWheel.values();
aWheel.selectValue(someVals[0]);
But instead I get the following error, logged in Instruments:
Exception raised while running script: - selectValue requires a valid value
Any ideas how I can
- Set predictable values on my UIPickerView cells/components? Currently, they all use custom UIViews, not the standard labels.
or
- Somehow get an array of values from my existing cells to iterate through?
What am I missing here?
回答1:
I also had this issue.
try var newValue = someVals[0].toString();
And then use the newValue
to select your value in your picker:
aWheel.selectValue(newValue);
This worked for me
回答2:
For me the selectValue
method on the picker wheel object didn't work if values in the wheel were empty, had spaces or had some special characters. I ended up having to use tapWithOptions({ tapOffset: { x: 0.23, y: 0.72 } })
instead.
回答3:
Your code looks correct. You need to add some checks though. I think your picker is empty, or you dont have a valid handler to it. Here is how I do it my code. Hope it helps.
target = UIATarget.localTarget();
app = target.frontMostApp();
mainWindow = app.mainWindow();
function selectOffice() {
UIALogger.logStart("selectOffice");
var validField = false;
try {
var picker = mainWindow.pickers()[0];
if (picker.isValid()) {
var wheel = picker.wheels()[0];
if (wheel.isValid()){
var pickedItems = wheel.values();
var nrOfItems = pickedItems.length;
if (nrOfItems > 0 ) {
wheel.selectValue(pickedItems[nrOfItems-1]);
validField = true;
}
}
}
}
catch(error) {
UIALogger.logFail(error);
}
if (validField == true ) {
UIALogger.logPass("selectOffice");
} else {
UIALogger.logFail("Couldnt find a valid control");
}
target.delay(1);
}
回答4:
SelectValue()
does not seem to work for text-values. It works for numeric, though.
For text-values, I just get the current value and tap until I find it.
来源:https://stackoverflow.com/questions/5523569/ui-automation-testing-how-do-i-select-uiapickerwheel-values