【问题标题】:How to drag and drop an element for xcode-ui-testing in Xcode 9如何在 Xcode 9 中拖放元素以进行 xcode-ui-testing
【发布时间】:2017-12-14 12:41:04
【问题描述】:

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

在将 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 中不起作用。

【问题讨论】:

  • 你能解释一下它是如何不工作的吗?运行测试时会发生什么?
  • 当执行sourceElement.press(forDuration: 0.5, thenDragTo: destElement)这行时返回失败并退出系统。
  • 它只发生在 Xcode 9 中
  • 什么故障?请在此处发布日志。
  • 日志有点长。请来聊天或将您的电子邮件地址发送给我。

标签: xcode xcode9 ios-ui-automation swift3.2


【解决方案1】:

实际上问题出在 iOS 11.0 中。

在 iOS 11.0 中,一些分层元素无法点击presslongpress

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

** 代码 sn-p 用于问题的解决方案

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

扩展方法如下

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)

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

【讨论】:

  • 感谢 Mahmud 和赞赏,它就像一个魅力。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-16
  • 2018-12-05
  • 1970-01-01
  • 2015-06-20
相关资源
最近更新 更多