Search code examples
swiftui-automationxctestios14

Unable to adjust the value of datepicker for UI testing: PickerWheel of type 6


We are writing UI automation for our iOS app using XCTest framework, where we have to select the date using datepicker. With iOS 13, we were using adjust() method to set the value of datepicker. But with iOS 14, the when we try to set the value for datepicker as below

element.adjust(toPickerWheelValue: "September")

I am getting following error

Unsupported picker wheel "2020" PickerWheel of type 6

Similar question has been posted by someone else at https://developer.apple.com/forums/thread/661809.

Has anyone encountered this issue?


Solution

  • There still doesn't seem to be a supported method of changing the new date picker wheels as of Xcode 12.1.

    I've come up with this method to get around the issue:

    func adjustDatePicker(wheel: XCUIElement, to newValue: String) -> Bool {    
        let x = wheel.frame.width / 2.0
        let y = wheel.frame.height / 2.0
        // each wheel notch is about 30px high, so tapping y - 30 rotates up. y + 30 rotates down.
        var offset: CGFloat = -30.0
        var reversed = false
        let previousValue = wheel.value as? String
        while wheel.value as? String != newValue {
            wheel.coordinate(withNormalizedOffset: .zero).withOffset(CGVector(dx: x, dy: y + offset)).tap()
            let briefWait = expectation(description: "Wait for wheel to rotate")
            briefWait.isInverted = true
            wait(for: [briefWait], timeout: 0.25)
            if previousValue == wheel.value as? String {
                if reversed {
                    // we already tried reversing, can't find the desired value
                    break
                }
                // we didn't move the wheel. try reversing direction
                offset = 30.0
                reversed = true
            }
        }
        
        return wheel.value as? String == newValue
    }