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

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

用于ios-app的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中不起作用。

xcode xcode9 ios-ui-automation swift3.2
1个回答
2
投票

实际问题是在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)

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

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