I have a picker view with few items: "Red", "Green", "Yellow", "Black". In my UI test I need to select a specific item "Green" from it. I am using the XCTest UI testing APIs that were intruduced with Xcode 7.
What I managed to do so far is to swipe the whole picker view up in the unit test. It is not ideal because it always changes the picker view to the bottom item (when swiping up).
let app = XCUIApplication() app.launch() app.pickers.elementAtIndex(0).swipeUp() XCTAssert(app.staticTexts["Selected: Black"].exists)
Another but very similar way of changing the picker view is to call pressForDuration ... thenDragToElement
, which is not what I want.
app.pickers.elementAtIndex(0).pressForDuration(0.1, thenDragToElement: someElement)
When I use the UI test record function it does not record the picker view scrolling events. It does record when I tap on picker view items:
app.pickerWheels["Green"].tap()
but that does not actually work when the test is run (probably because it needs to scroll the picker view first before tapping).
Here is the demo app with the test.
https://github.com/exchangegroup/PickerViewTestDemo
It is now possible to select a picker view since Xcode 7.0 beta 6 .
app.pickerWheels["Green"].adjustToPickerWheelValue("Yellow")
When you're ready to test, go to a test class and place the cursor inside the test method to record the interaction. From the debug bar, click the Record UI Test button. Xcode will launch the app and run it. You can interact with the element on-screen and perform a sequence of interactions for any UI test.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With