在 UI 测试中执行完整的向左滑动操作? [英] Perform a full swipe left action in UI Tests?
问题描述
我已经在表格视图中实现了前导和尾随滑动操作.现在,我正在尝试在 XCTest UI 测试中测试它们.
I have implemented leading and trailing swipe actions in a table view. Now, I'm trying to test them in XCTest UI tests.
要测试在任一方向上的常规滑动很容易:
To test a regular swipe in either direction is easy:
tableCell.swipeRight()
tableCell.swipeLeft()
使用其中之一会显示第一个操作按钮,然后我可以在按钮上.tap()
.
Using one of these causes the 1st action button to be displayed, and I can then .tap()
on the button.
然而,测试完全滑动被证明更具挑战性.我玩过这个扩展swift-how-do-i-swipe-faster-or-more-precisely">如何更快或更精确地滑动?
However, testing a full swipe is proving a little more challenging. I have played with this extension from How do I swipe faster or more precisely?
我也玩过 这个答案 问题问题/37158631/xcode7-ui-testing-statictextsxx-swiperight-swipes-not-far-enough">Xcode7 ui 测试:staticTexts[XX"].swipeRight() 滑动不够远.
I have also played with this answer from the question Xcode7 ui testing: staticTexts["XX"].swipeRight() swipes not far enough.
这两个基本上都使用 XCUIElement 的坐标(withNormalizedOffset:)从一个点滑动到另一个点的方法,类似如下:
Both of these essentially use XCUIElement's coordinate(withNormalizedOffset:) method to swipe from one point to another, similar to the following:
let startPoint = tableCell.coordinate(withNormalizedOffset: CGVector.zero)
let finishPoint = startPoint.withOffset(CGVector(dx:xOffsetValue, dy:yOffsetValue))
startPoint.press(forDuration: 0, thenDragTo: finishPoint)
我最终得到了一个扩展,它成功地执行了完全向右滑动 - 但我似乎无法为完全向左滑动获得正确的数字.
I ended up with an extension that successfully performs a full swipe right - but I can't seem to get the numbers right for a full swipe left.
我的代码确实向左滑动,但做得还不够远.我已经尝试了 dx:
从 -300 到 300 的硬编码数字.元素宽度是 414.我相信 0 是最左边的,而 414 是最右边的,所以我开始使用它尺寸作为参考.仍然没有快乐.
My code does perform a swipe left, but just doesn't go far enough. I've tried hardcoded numbers for dx:
from -300 to 300. The element width is 414. I believe that 0 is the far left, and 414 would be the far right, so I started using that size as a reference. Still, no joy.
我怎样才能让它向左完整滑动?
How can I get this to perform a full swipe left?
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)
}
}
推荐答案
知道了!
首先,我不需要使用单元格的框架大小.标准化"部分意味着您可以使用从 0.0 到 1.0 的值来表示元素大小的百分比.
First, I didn't need to use the frame size of the cell. The "normalized" part means that you can use values from 0.0 to 1.0 to represent a percentage of an element's size.
其次,调整大小,我发现长滑动需要移动单元格宽度的 60% 左右才能激活操作.
Second, playing with the sizes, I found a long swipe needs to move about 60% of the width of the cell in order to activate the action.
我由此产生的长滑动扩展:
My resulting long swipe extension:
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.zero
是 CGVector(dx: 0.0, dy: 0.0)
这篇关于在 UI 测试中执行完整的向左滑动操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!