【问题标题】:XCTest with touch gestures带有触摸手势的 XCTest
【发布时间】:2016-01-26 06:15:20
【问题描述】:

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

【问题讨论】:

    标签: ios xcode xctest touches


    【解决方案1】:

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

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

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

    关于#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 是类变量并在设置方法)

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

    希望这会有所帮助!

    达娜

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-29
      • 1970-01-01
      • 1970-01-01
      • 2015-10-21
      • 1970-01-01
      相关资源
      最近更新 更多