【问题标题】:Xcode7 ui testing: staticTexts["XX"].swipeRight() swipes not far enoughXcode7 ui 测试:staticTexts["XX"].swipeRight() 滑动不够远
【发布时间】:2016-05-11 09:38:05
【问题描述】:

对于 UI 测试,我想执行 swipeRight-gesture 以使更多按钮可以访问。我要滑动的元素位于屏幕底部。我通过以下方式访问它:

element.staticTexts["TEST TEXT"].swipeRight()

执行测试时,滑动不够远。它不会触发元素完全向右滑动并显示我想要点击的其他按钮。

我感觉swipeRight()抓住了静态文本的中间并执行了手势。

是否有可能将元素更向左抓取,使其向右滑动更多?

感谢您的任何建议!

【问题讨论】:

    标签: ios xcode xcode7 xcode-ui-testing ui-testing


    【解决方案1】:

    试试这个方法:

        let startPoint = element.staticTexts["TEST TEXT"].coordinateWithNormalizedOffset(CGVectorMake(0, 0)) // center of the element
        let finishPoint = startPoint.coordinateWithOffset(CGVectorMake(1000, 0))
        startPoint.pressForDuration(0, thenDragToCoordinate: finishPoint)
    

    你可以调整1000来达到你想要的效果。

    【讨论】:

    • 至少在 Xcode 8 和 Swift 3 中,1000 似乎比需要的值大得多。看起来 1.0 工作得很好,但我仍在尝试找出正在使用的坐标。有没有人有这方面的任何细节? 0,0 是被滑动的 UI 元素的中间吗?
    【解决方案2】:

    至少从 Swift 4.1、Xcode 9.4.1 开始,您需要的值代表元素宽度和高度的百分比 - 0.01.0

    对于长滑动,您需要在元素上拖动大约 60%,所以像这样:

    // right long swipe - move your 'x' from 0.0 to 0.6
    let startPoint = element.coordinate(withNormalizedOffset: CGVector(dx: 0.0, dy: 0.0))
    let endPoint = element.coordinate(withNormalizedOffset: CGVector(dx: 0.6, dy: 0.0))
    startPoint.press(forDuration: 0, thenDragTo: endPoint)
    

    和:

    // left long swipe - move your 'x' from 0.6 to 0.0
    let startPoint = element.coordinate(withNormalizedOffset: CGVector(dx: 0.6, dy: 0.0))
    let endPoint = element.coordinate(withNormalizedOffset: CGVector(dx: 0.0, dy: 0.0))
    startPoint.press(forDuration: 0, thenDragTo: endPoint)
    

    您可以将dy 值设置为0.5,例如,如果您想在元素的中心拖动。但是,请注意,您的 dy 值必须相同才能向右或向左滑动。如果您要将其转换为向上或向下滑动,dx 值需要相同,而dy 值将相应更改。 (滑动需要沿直线移动。)

    另外请注意,forDuration 值表示在拖动之前保持触摸的秒数。值越高,初始按下的时间越长。

    我在这个answer 中发布了一个XCUIElement 长滑动扩展。

    【讨论】:

    • 感谢您的提示:“从 0.6 到 0.0”左滑!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-25
    • 1970-01-01
    • 1970-01-01
    • 2015-10-23
    • 1970-01-01
    相关资源
    最近更新 更多