2

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)

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

4

1 回答 1

5

实际上问题出在 iOS 11.0 中。

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

您必须使用元素中心点来进行点击按下长按

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

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

扩展方法如下

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)
  }

}

然后像下面这样调用扩展方法

 sourceElement.dragAndDropUsingCenterPos(forDuration: 0.5, thenDragTo: destElement)

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

于 2017-12-15T13:48:40.810 回答