How to Select a Picker View Item in an iOS UI Test in Xcode

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

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.

iOS UI Test to adjust picker value on SwiftUI app

The behaviour of adjust(toPickerWheelValue:) seems a bit counterintuitive. You could write your own function to accomplish the task:

extension XCUIElement{

func selectPicker(value: String, timeout: TimeInterval) {
let pickerWheel = pickerWheels.firstMatch
let row = pickerWheels[value]

while !row.waitForExistence(timeout: timeout) {
pickerWheel.adjust(toPickerWheelValue: value)
}
}
}

and then use it like this:

let picker = app.pickers["picker"]
picker.selectPicker(value: "Purple", timeout: 1)

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

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.

UIPickerView and UI Unit Test: How to get values of UIPickerWheel?

This is probably not the answer you were hoping for but it is not possible to get the title of all rows in a UIPickerView in a UITest.

As you know when running a UITest you can only access your app's UI elements via the XCUIElement class. That class has a value property that gives you some information about the UI element you access. When accessing a UIPickerView the value gives you the title of the currently selected row. But only of the selected row. You can access the picker's row elements, but unfortunately the value property for the row elements is always empty. So, no luck here. All you the info you can get is the number of rows of your picker.

This is not really surprising though. Even if you had access to the UIPickerView, you could not access the titles of all rows directly. UIPickerView does not know about the titles that it displays. It is the job of the UIPickerViewDataSource to provide the titles.

So, unfortunately, if you need to know all the row titles of your UIPickerView in a UITest, you really have to select each value one by one via your app's user interface.

But it does not have to be as complicated as in the answer you linked. Instead of simulating a scroll you can simply tap on the next row to select it (should be slightly faster):

let pickerView = app.pickerWheels.element
let numRows = pickerView.children(matching: .any).count
var values: [String] = [pickerView.value as! String]

for _ in 0..<numRows {
pickerView.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.55)).tap()
values.append(pickerView.value as! String)
}

In your question you do not really describe what you are trying to test. But if you want to test if the picker has the correct row titles maybe a UnitTest would be a more practical approach?



Related Topics



Leave a reply



Submit