Xcode UI 测试:如何将 XCUIElement 移动到屏幕上的另一个位置?

问题描述 投票:0回答:1

我的应用程序显示地图注释,可以将其拖到地图的另一个位置。我想编写一个 UI 测试来检查拖动停止时发生的情况。

我找到了一种方法(参见文档)使用

将一个
XCUIElement
拖到另一个
XCUIElement

pressForDuration:thenDragToElement:withVelocity:thenHoldForDuration:

我还找到了一种方法(见这里)通过按住并拖动坐标来拖动地图。

但是我没有找到将元素从当前坐标移动到另一个坐标的方法。这需要长按元素,然后将其拖动到目标坐标。

如何做到这一点?

drag xcode-ui-testing xcuielement
1个回答
0
投票

问题解决:
我使用了this answer中的代码,应用于我的

XCUIElement
pin

let start  = pin.coordinate(withNormalizedOffset: CGVector(dx: 0.0, dy: 0.0))
let finish = pin.coordinate(withNormalizedOffset: CGVector(dx: 3.0, dy: 3.0))
start.pressForDuration(1.0, thenDragToCoordinate: finish)  

但我不知道归一化偏移量是相对于元素原点的。
因此,

CGVector(dx: 0.0, dy: 0.0)
的归一化偏移量是元素的确切来源,此时的按压显然被视为对背景的按压,而不是对元素本身的按压。因此,我的地图被拖了。

我用的时候

let start  = pin.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.5))

元素在其中心被按下,元素被拖动。

© www.soinside.com 2019 - 2024. All rights reserved.