【发布时间】: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