4

I'm trying to create XCTestCase to test reordering in my outline view (in OS X app). When I use UI Test Recording feature, Xcode prints this:

window.outlines.staticTexts["<cell which I want to move>"].click()

I tried dragging the cell both inside or outside outline view, Xcode generates the same useless code.

Does anyone know how to test drag & drop correctly in Xcode 7?

egor.zhdan
  • 4,555
  • 6
  • 39
  • 53

4 Answers4

9

Not sure if this is new, but in Xcode 7.2, if you look at the header for XCUIElement, there's a clickForDuration(thenDragToElement:) function for OS X. Seems to work for me with NSOutlineView.

This is from the header...

/*!
 * Clicks and holds for a specified duration (generally long enough to start a drag operation) then drags
 * to the other element.
 */
public func clickForDuration(duration: NSTimeInterval, thenDragToElement otherElement: XCUIElement)
harrisg
  • 613
  • 7
  • 12
  • Works like a charm, thanks! Haven't seen this function before, perhaps it was added in Xcode 7.2 – egor.zhdan Jan 07 '16 at 20:21
  • Also works with Xcode 8/ Swift 3, but the function is: `func press(forDuration duration: TimeInterval, thenDragTo otherElement: XCUIElement)`. With Swift 2.3 it is `func pressForDuration(duration: TimeInterval, thenDragToElement otherElement: XCUIElement)` – Daniel Sep 26 '16 at 13:58
2

With Xcode 9.4.1 Swift:

func press(forDuration duration: TimeInterval, thenDragTo otherElement: XCUIElement)

Example:

let ele: XCUIElement = XCUIApplication()!.otherElements.matching(identifier: "element_identifier").element(boundBy: 0)
ele.press(forDuration: <timeIntervalInSec>, thenDragTo: <destination_XCUIElement>)

Refer: API Doc

Sravan
  • 591
  • 1
  • 4
  • 16
1

Same issue. No solution. Looks like accessibility does not support d-n-d, and Apple docs say: Provide alternatives for drag-and-drop operations.

Daniel Wexler
  • 131
  • 1
  • 7
0

The only thing I can suggest is to try checking the accessibility features of the code being tested. UI Testing needs to have accessibility information to be able to understand what is happening in the UI. See the Apple WWDC15 UI Testing video at about 39:30 into the video for an example of how to fix issues where UI recording can't properly generate code.

James C
  • 366
  • 4
  • 6