xcodexcode9ios-ui-automationswift3.2

How to drag and drop an element for xcode-ui-testing in Xcode 9


UI Test for an ios-app, developed in Xcode 8 and swift 3.2.

I'm facing a problem to deal with drag and drop after upgrading Xcode version 8 to 9

I would like to drag an element [i.e. button] and drop it to another element [i.e. on homepage].

For Xcode 8, I implemented it using below method:

let sourceElement = app.buttons["Video_Button"]

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

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

But the above code is not working in Xcode 9 and Swift 3.2.


Solution

  • Actually issue is in iOS 11.0.

    In iOS 11.0 some hierarchical elements failed to tap or press or longpress.

    You have to work with the elements center point to do the tap or press or longpress.

    ** Code snippet for the Solution of the problem is pasted below.

    First create an extension method for press on a center of the element and drag this element to the destination elements center.

    Extension method is below

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

    Then call the extension method like below

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

    Hope it will solve your issue. Let me know your feedback.