如何使用xctest滚动到特定位置

时间:2017-01-06 12:24:04

标签: ios objective-c xctest

我正在尝试向我的应用中滑动到某个特定元素,如果我使用“swipeup”它会转到视图的底部,我不想要

这是我的代码:

XCUIElement *staticText = [[[tablesQuery2 childrenMatchingType:XCUIElementTypeCell] elementBoundByIndex:2] childrenMatchingType:XCUIElementTypeStaticText].element;
[staticText swipeUp]; 

以下是使用向上滑动之前的应用屏幕 enter image description here 使用向上滑动后,这是我的应用屏幕 enter image description here

1 个答案:

答案 0 :(得分:0)

如果您知道要选择哪个元素以及每个选择器项的高度,您可以对XCUIElement进行扩展以选择正确的值。

/// Move up/down the picker options until the given `selectionPosition` is reached.
func changePickerSelection(pickerWheel: XCUIElement, selectionPosition: UInt) {
    // Select the new value
    var valueSelected = false
    while !valueSelected {
        // Get the picker wheel's current position
        if let pickerValue = pickerWheel.value {
            let currentPosition = UInt(getPickerState(String(pickerValue)).currentPosition)
            switch currentPosition.compared(to: selectionPosition) {
            case .GreaterThan:
                pickerWheel.selectPreviousOption()
            case .LessThan:
                pickerWheel.selectNextOption()
            case .Equal:
                valueSelected = true
            }
        }
    }
}

/// Extend XCUIElement to contain methods for moving to the next/previous value of a picker.
extension XCUIElement {
    /// Scrolls a picker wheel up by one option.
    func selectNextOption() {
        let startCoord = self.coordinateWithNormalizedOffset(CGVector(dx: 0.5, dy: 0.5))
        let endCoord = startCoord.coordinateWithOffset(CGVector(dx: 0.0, dy: 30.0)) // 30pts = height of picker item
        endCoord.tap()
    }

    /// Scrolls a picker wheel down by one option.
    func selectPreviousOption() {
        let startCoord = self.coordinateWithNormalizedOffset(CGVector(dx: 0.5, dy: 0.5))
        let endCoord = startCoord.coordinateWithOffset(CGVector(dx: 0.0, dy: -30.0))
        endCoord.tap()
    }
}

let pickerWheel = app.pickerWheels.element(boundBy: 0)
changePickerSelection(pickerWheel, selectionPosition: 2)