【问题标题】:Perform a full swipe left action in UI Tests?在 UI 测试中执行完全向左滑动操作?
【发布时间】:2018-07-31 20:18:58
【问题描述】:

我已经在表格视图中实现了前导和尾随滑动操作。现在,我正在尝试在 XCTest UI 测试中测试它们。

测试任意方向的常规滑动很容易:

tableCell.swipeRight()
tableCell.swipeLeft()

使用其中一个会显示第一个操作按钮,然后我可以在按钮上.tap()

但是,事实证明,测试完整的滑动更具挑战性。我玩过来自How do I swipe faster or more precisely?this extension

我也玩过this answer这个问题Xcode7 ui testing: staticTexts[“XX”].swipeRight() swipes not far enough

这两者本质上都是使用XCUIElement's coordinate(withNormalizedOffset:) 方法从一个点滑动到另一个点,类似于以下:

let startPoint = tableCell.coordinate(withNormalizedOffset: CGVector.zero)
let finishPoint = startPoint.withOffset(CGVector(dx:xOffsetValue, dy:yOffsetValue))
startPoint.press(forDuration: 0, thenDragTo: finishPoint)

我最终得到了一个扩展,它成功地执行了完全向右滑动 - 但我似乎无法为 完全 滑动向左获得正确的数字

我的代码确实执行了向左滑动,但还不够远。我尝试了从 -300 到 300 的 dx: 的硬编码数字。元素宽度为 414。我相信 0 是最左边,而 414 是最右边,所以我开始使用该大小作为参考。不过,还是不开心。

我怎样才能让它向左完全滑动?

extension XCUIElement
{
    enum SwipeDirection {
        case left, right
    }

    func longSwipe(_ direction : SwipeDirection) {
        let elementLength = self.frame.size.width
        let centerPoint: CGFloat = elementLength / 2.0
        let halfCenterValue: CGFloat = centerPoint / 2.0

        let startOffset: CGVector
        let endOffset: CGVector
        switch direction {
        case .right:  // this one works perfectly!
            startOffset = CGVector.zero
            endOffset = CGVector(dx: centerPoint + halfCenterValue, dy: 0)
        }
        case .left:  // "There's the rub" as Hamlet might say...
            startOffset = CGVector(dx: centerPoint + halfCenterValue, dy: 0)
            endOffset = CGVector.zero

        let startPoint = self.coordinate(withNormalizedOffset: startOffset)
        let finishPoint = startPoint.withOffset(endOffset)
        startPoint.press(forDuration: 0, thenDragTo: finishPoint)
    }
}

【问题讨论】:

    标签: ios swift xctest xcode-ui-testing


    【解决方案1】:

    知道了!

    • 首先,我不需要使用单元格的帧大小。 “标准化”部分意味着您可以使用从 0.01.0 的值来表示元素大小的百分比。

    • 其次,玩转尺寸,我发现长滑动需要移动大约 60% 的单元格宽度才能激活动作。

    我生成的长滑动扩展:

    extension XCUIElement
    {
        enum SwipeDirection {
            case left, right
        }
    
        func longSwipe(_ direction : SwipeDirection) {
            let startOffset: CGVector
            let endOffset: CGVector
    
            switch direction {
            case .right:
                startOffset = CGVector.zero
                endOffset = CGVector(dx: 0.6, dy: 0.0)
            case .left:
                startOffset = CGVector(dx: 0.6, dy: 0.0)
                endOffset = CGVector.zero
            }
    
            let startPoint = self.coordinate(withNormalizedOffset: startOffset)
            let endPoint = self.coordinate(withNormalizedOffset: endOffset)
            startPoint.press(forDuration: 0, thenDragTo: endPoint)
        }
    }
    

    注意:CGVector.zeroCGVector(dx: 0.0, dy: 0.0) 的 Swift 别名

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-10
      • 2017-04-04
      • 2018-02-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-28
      • 2011-07-23
      相关资源
      最近更新 更多