gpt4 book ai didi

ios - XCTest Swift - 如何更快或更精确地滑动?

转载 作者:IT王子 更新时间:2023-10-29 05:46:06 24 4
gpt4 key购买 nike

我在 XCTest 中使用 Swift 进行了测试,我试图在提要中滑动直到满足条件。我正在使用 swipeUp(),但问题在于它的行为就像人类的滑动,并且在放手时会出现减速的痛苦动画。在减速动画完成之前,它不会再次尝试滑动。此外,该方法不带任何参数。我想看看是否有类似 Calabash 的东西,或者甚至是 Androids 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
}
}
}

关于ios - XCTest Swift - 如何更快或更精确地滑动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39280821/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com