How to select a picker view item in an iOS UI test in Xcode?

无人久伴 提交于 2019-11-27 22:39:31

As noted in the question's update, Xcode 7 Beta 6 added support for interacting with pickers. The newly added method -adjustToPickerWheelValue: should be used to select items on a UIPickerView.

let app = XCUIApplication()
app.launch()
app.pickerWheels.element.adjustToPickerWheelValue("Yellow")

Here's a GitHub repo with a working example. And some more information in a blog post I wrote.

If there are multiple wheels, an easy way to select items it is like this:

precondition: it's a date picker (UIDatePicker), swift language

    app.pickerWheels.elementBoundByIndex(0).adjustToPickerWheelValue("March")
    app.pickerWheels.elementBoundByIndex(1).adjustToPickerWheelValue("13")
    app.pickerWheels.elementBoundByIndex(2).adjustToPickerWheelValue("1990")

where: index "0" is month, "1" is day, "2" is year

Swift 4 version of @Joao_dche's answer

app.pickerWheels.element(boundBy: 0).adjust(toPickerWheelValue: "March")
app.pickerWheels.element(boundBy: 1).adjust(toPickerWheelValue: "13")
app.pickerWheels.element(boundBy: 2).adjust(toPickerWheelValue: "1990")

Objective-C Version of @Joao_dche's Answer

Using UIDatePicker with XCTest for UI Testing:

    XCUIElementQuery *datePickersQuery = app.datePickers;
    [datePickersQuery.pickerWheels.allElementsBoundByIndex[0] adjustToPickerWheelValue:@"May"];
    [datePickersQuery.pickerWheels.allElementsBoundByIndex[1] adjustToPickerWheelValue:@"13"];
    [datePickersQuery.pickerWheels.allElementsBoundByIndex[2] adjustToPickerWheelValue:@"2017"];

    XCUIElement *doneButton = app.buttons[@"Done"];
    [doneButton.firstMatch tap];

Joe's answer working great on iOS 11, but doesn't work for me on iOS 10.3.1 [Xcode 9.2]

I use custom views (labels actually) as picker rows and unfortunately gorgeous Joe's answer doesn't work for me on iOS 10.3.1 while working perfectly on iOS 11. So I had to use

app.pickerWheels["X, Y of Z"].swipeUp()

where

X is my label's text.
Y is row position in picker view. Position number starts with 1, not zero.
Z is number of values in picker view.

So in my case this command looks like

app.pickerWheels["1st, 1 of 28"].swipeUp()

This is one of most popular post for select pickervie(UIPickerView)

If you want to select state/date from picker view. You can try following swift 3 code.

 XCUIApplication().tables.pickerWheels["AK"].adjust(toPickerWheelValue: "MA")

PickerWheels pick from AK and set the target state.

XCUIApplication().tables.pickerWheels["Starting Point" "].adjust(toPickerWheelValue: "Target Place")

I used below code to identify the element displayed now in the picker wheel.

To Find an element in picker

let valueSelected = algorithmsPicker.pickerWheels.element(boundBy: 0).value as! String

To select a value in the picker wheel:

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