我在XCTest中使用Swift进行了测试,并且试图在Feed中滑动,直到满足条件为止。我正在使用swipeUp()
,但问题是它的行为就像人的滑动一样,放开时会出现这种令人痛苦的动画放慢速度。在减速动画完成之前,它不会再次尝试滑动。此外,该方法不带任何参数。我想看看是否有类似Calabash的东西,或者甚至拍摄了Android Espresso,那里有诸如swipeUp(fast)
或swipeUp(medium)
甚至swipeUp(x0,y200)
的滑动方法的属性。
这是我想在代码中执行的操作:
func scrollDownUntilYouFindSomething() {
while !findingSomehting.exists {
app.swipeUp()
}
}
很简单,但是XCUIApplication中的swipeUp()非常慢。我希望能够以精确度甚至是坐标滑动。我尝试使用从Replicate pull to refresh in XCTest UI testing采取的坐标方法
但是放入循环时同样慢。
最佳答案
我根据Bharathram C给出的出色答案编写了扩展程序。
我发现它比XCUIElement.swipeUp()轻扫得多
// XCUIElement+GentleSwipe.swift
import Foundation
import XCTest
extension XCUIElement
{
enum direction : Int {
case Up, Down, Left, Right
}
func gentleSwipe(_ direction : direction) {
let half : CGFloat = 0.5
let adjustment : CGFloat = 0.25
let pressDuration : TimeInterval = 0.05
let lessThanHalf = half - adjustment
let moreThanHalf = half + adjustment
let centre = self.coordinate(withNormalizedOffset: CGVector(dx: half, dy: half))
let aboveCentre = self.coordinate(withNormalizedOffset: CGVector(dx: half, dy: lessThanHalf))
let belowCentre = self.coordinate(withNormalizedOffset: CGVector(dx: half, dy: moreThanHalf))
let leftOfCentre = self.coordinate(withNormalizedOffset: CGVector(dx: lessThanHalf, dy: half))
let rightOfCentre = self.coordinate(withNormalizedOffset: CGVector(dx: moreThanHalf, dy: half))
switch direction {
case .Up:
centre.press(forDuration: pressDuration, thenDragTo: aboveCentre)
break
case .Down:
centre.press(forDuration: pressDuration, thenDragTo: belowCentre)
break
case .Left:
centre.press(forDuration: pressDuration, thenDragTo: leftOfCentre)
break
case .Right:
centre.press(forDuration: pressDuration, thenDragTo: rightOfCentre)
break
}
}
}