XCTest Swift-如何更快或更精确地滑动? [英] XCTest Swift - How do I swipe faster or more precisely?
问题描述
我在XCTest中使用Swift进行了测试,我试图在Feed中滑动,直到满足条件为止.我正在使用swipeUp()
,但问题是它的动作就像人的滑动动作一样,放开时会放慢脚步,令人痛苦.在减速动画完成之前,它不会再次尝试滑动.此外,该方法不带任何参数.我想看看是否有类似Calabash的东西,或者甚至拍摄了Android Espresso,其中有swipeUp(fast)
或swipeUp(medium)
甚至swipeUp(x0,y200)
这样的滑动方法的属性.
I have tests in XCTest using Swift and I am trying to swipe through a feed until a condition is met. I am using the swipeUp()
, but the problem with this is it acts like a human swipe and has this painful animation of slowing down when it lets go. It doesn't try the swipe again until the slowing-down-animation is complete. Additionally, that method takes no arguments. I would like to see if there is something like Calabash or shoot even Androids Espresso where there are properties to a swipe method like swipeUp(fast)
or swipeUp(medium)
or even swipeUp(x0,y200)
.
这是我要在代码中尝试做的事情:
Here is what I am trying to do in code:
func scrollDownUntilYouFindSomething() {
while !findingSomehting.exists {
app.swipeUp()
}
}
足够简单,但是XCUIApplication中的swipeUp()非常慢.我希望能够以精确度甚至是坐标滑动.我尝试使用来自复制拉动以在XCTest UI中刷新的坐标方法测试
Simple enough, but the swipeUp() in XCUIApplication is painfully slow. I want to be able to swipe with precision and even by coordinate. I tried to use the coordinate approach taken from Replicate pull to refresh in XCTest UI testing
,但是放入循环时同样慢.
but its equally as slow when put into a loop.
推荐答案
我基于Bharathram C给出的出色答案编写了扩展.
I wrote an extension based on the excellent answer given by Bharathram C.
我发现它比XCUIElement.swipeUp()轻扫得多
I find it to be a much gentler swipe than 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
}
}
}
这篇关于XCTest Swift-如何更快或更精确地滑动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!