【问题标题】:how to scroll till particular position using xctest如何使用 xctest 滚动到特定位置
【发布时间】:2017-01-06 12:24:04
【问题描述】:

我正在尝试向上滑动到我的应用程序中的特定元素,如果我使用“swipeup”,它将进入视图的底部,这是我不想要的

这是我的代码:

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

这是我使用向上滑动之前的应用屏幕 这是我使用向上滑动后的应用屏幕

【问题讨论】:

  • 如果您的滚动条包含与您正在使用的视图相同的高度使用标签,请使用该标签将视图与该标签相乘以使内容偏移,否则如果视图包含不规则高度,则获取每个视图和滚动条的高度,并且它们直到空间视图,然后制作内容大小。
  • 我听不懂@AbuUlHassan,你是说我必须使用坐标然后滚动到那里吗?举个例子[[[app coordinateWithNormalizedOffset:CGVectorMake(0.0, 94.0)] coordinateWithOffset:CGVectorMake(375.0,44.0)] tap];
  • 不不,我是说循环滚动子视图获取它们的高度,直到找不到特定视图并设置 scroll.contentOffset.y = computedheight

标签: ios objective-c xctest


【解决方案1】:

如果您知道要选择哪个元素以及每个选择器项的高度,则可以对 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-19
    • 1970-01-01
    相关资源
    最近更新 更多