如何在 Xcode 9 中拖放用于 xcode-ui-testing 的元素

Ali*_*zam 2 xcode ios-ui-automation swift3.2 xcode9

ios 应用程序的 UI 测试,在 Xcode 8 和 swift 3.2 中开发。

将 Xcode 版本 8 升级到 9 后,我面临处理拖放的问题

我想将一个元素[i.e. button]拖放到另一个元素上[i.e. on homepage]

对于 Xcode 8,我使用以下方法实现了它:

let sourceElement = app.buttons["Video_Button"]

let destElement = app.scrollViews.scrollViews.images.element(boundBy: 0)

sourceElement.press(forDuration: 0.5, thenDragTo: destElement)
Run Code Online (Sandbox Code Playgroud)

但是上面的代码在 Xcode 9 和 Swift 3.2 中不起作用。

Mah*_*iad 5

实际上问题出在 iOS 11.0 中。

在 iOS 11.0 中,一些分层元素无法点击按下长按

您必须使用元素中心点来执行tappresslongpress

** 代码片段解决该问题的粘贴下面。

首先创建一个用于按下元素中心的扩展方法并将该元素拖动到目标元素中心。

扩展方法如下

extension XCUIElement {

func dragAndDropUsingCenterPos(forDuration duration: TimeInterval,
                               thenDragTo destElement: XCUIElement) {

    let sourceCoordinate: XCUICoordinate = self.coordinate(withNormalizedOffset: CGVector(dx:0.5, dy:0.5))

    let destCorodinate: XCUICoordinate = destElement.coordinate(withNormalizedOffset: CGVector(dx:0.5, dy:0.5))

    sourceCoordinate.press(forDuration: duration, thenDragTo: destCorodinate)
  }

}
Run Code Online (Sandbox Code Playgroud)

然后调用如下扩展方法

 sourceElement.dragAndDropUsingCenterPos(forDuration: 0.5, thenDragTo: destElement)
Run Code Online (Sandbox Code Playgroud)

希望它能解决你的问题。让我知道您的反馈。