带有触摸手势的 XCTest

XCTest with touch gestures

我想测试一个使用 XCTest 记录用户在屏幕上绘制手势的应用程序。我想知道使用 XCode 7 中引入的新 UI 测试是否完全可行。我浏览了整个视频进行解释,大部分讨论仅限于在按钮上生成点击事件。

我想到了两种可能会解决您的问题的方法,具体取决于您的问题有多复杂以及您的偏好。

  1. 在 UITests 中,您可以使用一些手势,例如 .swipeLeft() 或 pinchWithScale(2.0, velocity: 1),或 .tap() 或 pressForDuration(2.0)。虽然通常如果您尝试记录您的测试,它可能无法捕获滑动或捏合,因此您很可能需要重构代码以使用滑动或捏合。

  2. 在单元测试中,您可以模拟您正在测试的 class 并在该模拟中满足一些期望。不过,您需要提供更多代码。

关于#1。 UITests(因为我觉得那是您正在寻找的解决方案)-我最常使用它的方式:(A)如果在创建并呈现该手势后出现新视图或弹出窗口,您可以尝试像这样测试该新视图:

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) 如果在您滑动标签更改后,您可以查找标签值并与旧值进行比较(顺便说一句。这里我假设 appmyOldView 是 class 变量并在 setUp 方法中实例化)

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

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

希望对您有所帮助!

达娜