swiftxcodeios-ui-automation

Is there a way to get the string value of a picker wheel that is an XCUIElement?


I know that this exists: func adjust(toPickerWheelValue pickerWheelValue: String), but I'm not trying to adjust the value. I want to make sure that the value is the string that I'm expecting after I've moved it.

I tried doing .value on the picker wheel element, but it's giving me an Any type with the string that I need in there, plus some extra stuff.

Let's say the printout of the screen is this:

Attributes: Application, pid: 64445, label: ‘App’
Element subtree:
 →Application, 0x60000263a150, pid: 64445, label: ‘App’
    Window (Main), 0x600002639110, {{0.0, 0.0}, {320.0, 568.0}}
      Other, 0x6000026396c0, {{0.0, 0.0}, {320.0, 568.0}}
        Picker, 0x600002639040, {{0.0, 352.0}, {320.0, 216.0}}
            PickerWheel, 0x600002639520, {{0.0, 314.5}, {103.0, 291.0}}, value: 0 hours
            PickerWheel, 0x600002639380, {{108.0, 314.5}, {103.0, 291.0}}, value: 13 min

I'll print out app.pickerWheels.element(boundBy: 0).value in the console and get this

Optional<Any>
    - some : 0 hours

I explicitly only want the 0 hours so I can assert that the picker wheel says that.


Solution

  • since I was having the same problem I found your question here. And now I found a solution as well from here: How to select a picker view item in an iOS UI test in Xcode? (the last answer).

    It is possible to cast the value to String like this:

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

    This gave me the correct value to continue.

    Hope I was able to help, since your question helped me as well and it made me sign up here to give the answer.