UI Automation Testing: How do I select UIAPickerWheel values?

最后都变了- 提交于 2019-12-06 13:06:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!