[英]Perform a full swipe left action in UI Tests?
I have implemented leading and trailing swipe actions in a table view. 我已经在表格视图中实现了前导和尾随滑动操作。 Now, I'm trying to test them in XCTest UI tests.
现在,我正在尝试在XCTest UI测试中对其进行测试。
To test a regular swipe in either direction is easy: 要测试任一方向的常规滑动都很容易:
tableCell.swipeRight()
tableCell.swipeLeft()
Using one of these causes the 1st action button to be displayed, and I can then .tap()
on the button. 使用其中之一会导致显示第一个操作按钮,然后我可以在该按钮上使用
.tap()
。
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?
我有玩过这个扩展从如何刷卡更快或更精确?
I have also played with this answer from the question Xcode7 ui testing: staticTexts[“XX”].swipeRight() swipes not far enough . 我还从问题Xcode7 ui测试中得到了这个答案 :staticTexts [“ XX”]。swipeRight()滑动距离不够 。
Both of these essentially use XCUIElement's coordinate(withNormalizedOffset:) method to swipe from one point to another, similar to the following: 两者本质上都使用XCUIElement的坐标(withNormalizedOffset :)方法从一个点滑动到另一点,类似于以下内容:
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 . 我最终得到了一个扩展名,该扩展名成功了向右完全滑动 -但是我似乎无法为左 完全滑动获得正确的数字。
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. 我尝试了
dx:
硬编码数字dx:
从-300到300。元素宽度为414。我认为最左端为0,而最右端为414,因此我开始使用该大小作为参考。 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)
}
}
Got it! 得到它了!
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.
“标准化”部分意味着您可以使用0.0到1.0之间的值来表示元素尺寸的百分比。
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. 其次,在进行尺寸调整时,我发现需要长滑动才能移动单元格宽度的60%左右才能激活动作。
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)
}
}
Note: CGVector.zero
is a Swift alias for CGVector(dx: 0.0, dy: 0.0)
注意:
CGVector.zero
是CGVector(dx: 0.0, dy: 0.0)
的Swift别名
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.