Las*_*awk 9 xcode ios xctest swift
我在XCTest中使用Swift进行了测试,我正在尝试滑动一个Feed,直到满足条件.我正在使用它swipeUp(),但问题在于它的行为就像人类轻扫一样,并且当它放开时会有这种痛苦的动画减速.在减速动画完成之前,它不会再次尝试滑动.此外,该方法不带任何参数.我想看看是否有像葫芦或拍摄甚至机器人会咖啡那里有属性刷卡方法类似swipeUp(fast)或者swipeUp(medium)甚至是swipeUp(x0,y200).
这是我在代码中尝试做的事情:
func scrollDownUntilYouFindSomething() {
while !findingSomehting.exists {
app.swipeUp()
}
}
Run Code Online (Sandbox Code Playgroud)
很简单,但XCUIApplication中的swipeUp()非常慢.我希望能够精确地滑动甚至通过坐标.我试图在XCTest UI测试中使用从Replicate pull获取的坐标方法进行刷新
但是当它进入循环时它同样缓慢.
Ugl*_*Cat 12
我根据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
}
}
}
Run Code Online (Sandbox Code Playgroud)
是的,我们可以在Swift中进行基于坐标的滑动.我们可以使用pressForDuration(duration,thenDragToElement:XCElement)创建自己的滑动操作.请看下面的例子.
为此,我们需要
然后我们可以有一个类似于下面的函数(在Swift 3中)
func customSwipe(refElement:XCUIElement,startdelxy:CGVector,enddeltaxy: CGVector){
let swipeStartPoint = refElement.coordinate(withNormalizedOffset: startdelxy)
let swipeEndPoint = refElement.coordinate(withNormalizedOffset: enddeltaxy)
swipeStartPoint.press(forDuration: 0.1, thenDragTo: swipeEndPoint)
}
Run Code Online (Sandbox Code Playgroud)
如果startdelxy是(0.0,0.0)并且enddeltaxy是(0.0,-ve),则滑动将从refElement向上移动.如果enddeltaxy是(0.0,+ ve),则滑动将下降.这是因为我们的参考元素将是原点,X是+ ve向右,Y是+ ve低于我们的参考元素.从较小的Y值开始(比如-0.75)并根据需要进行更改.更多的差异,更长的刷卡.
的swipeStartPoint和swipeEndPoint可以用元件更换(如果这样的存在于图).通常,对于swipeEndPoint,我们可以在导航栏中搜索swipeUp操作的元素.此外,更改X可以在左右方向上为我们提供customSwipes.
至于刷卡的速度,我不知道任何可以刷卡比默认速率更快/更慢的API.
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)
case .down:
centre.press(forDuration: pressDuration, thenDragTo: belowCentre)
case .left:
centre.press(forDuration: pressDuration, thenDragTo: leftOfCentre)
case .right:
centre.press(forDuration: pressDuration, thenDragTo: rightOfCentre)
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5201 次 |
| 最近记录: |