nishant
nishant

Reputation: 756

XCTest with touch gestures

I would like to test an application that records user draw gestures on the screen with XCTest. I wonder if that's at all possible using the new UI testing that was introduced in XCode 7. I went through the entire video explaining and much of the discussion was only limited to generating tap events on buttons.

Upvotes: 1

Views: 4160

Answers (1)

da-na
da-na

Reputation: 250

There are two ways that come to my mind, that might solve your problem, depending how complicated is your problem and what's you preference.

  1. In UITests you have some gestures available like .swipeLeft() or pinchWithScale(2.0, velocity: 1), or .tap(), or pressForDuration(2.0). Though usually if you try to record your test, it might not catch the swipes or pinches, so you will most likely need to refactor your code to use swipes or pinches.

  2. In Unit Tests you could mock the class that you're testing and fulfill some expectations within that mock. You would need to provide more code though.

With respect to #1. UITests (since I feel that was the solution you were looking for) - The way I use it most often: (A) if a new view or popup after that gesture is created and presented, you could try testing that new view like that:

func testThatAfterLongPressMyNewViewIsPresented() {
  let app = XCUIApplication()
  let window = app.childrenMatchingType(.Window).elementBoundByIndex(0)
  let myOldView = window.childrenMatchingType(.Other).elementBoundByIndex(1).childrenMatchingType(.Other).element
  let myNewView = window.childrenMatchingType(.Other).elementBoundByIndex(2).childrenMatchingType(.Other).elementBoundByIndex(1)

  XCTAssertFalse(myNewView.exists)
  myOldView.pressForDuration(2.0)
  XCTAssertTrue(myNewView.exists)
}

(B) if after you swipe a label changes, you can lookup the label value and compare to the old value (btw. here I'm assuming that app and myOldView are class variables and were instantiated in the setUp method)

func testThatAfterSwipeLabelChanges() {
  let myTextLabel = app.textFields.matchingIdentifier("textLabelId")
  let currentlyDisplayedText = myTextLabel.label

  myOldView.swipeLeft()
  XCTAssertNotEqual(currentlyDisplayedText, myTextLabel.label)
}

Hope this helps!

da-na

Upvotes: 2

Related Questions