XCTest带有触摸手势

时间:2016-01-26 06:15:20

标签: ios xcode xctest touches

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

1 个答案:

答案 0 :(得分:2)

有两种方式可以解决您的问题,这取决于您的问题有多复杂以及您的偏好。

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

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

  3. 关于#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是类变量并在setUp方法中实例化)

    func testThatAfterSwipeLabelChanges() {
      let myTextLabel = app.textFields.matchingIdentifier("textLabelId")
      let currentlyDisplayedText = myTextLabel.label
    
      myOldView.swipeLeft()
      XCTAssertNotEqual(currentlyDisplayedText, myTextLabel.label)
    }
    

    希望这有帮助!

    DA-NA