使用6次重复测试TapGesture(iOS和Swift)

时间:2016-08-31 15:41:29

标签: ios swift xcode integration-testing

这可能很有趣。我正在XCode中设置UI测试。 我的应用程序的一部分要求用户点击屏幕6次以执行特定操作。代码如下。

        let tapGesture = UITapGestureRecognizer(target: self,action:#selector(self.doSomething(_:)))
        tapGesture.numberOfTapsRequired = 6
        aView.addGestureRecognizer(tapGesture)

我遇到问题的地方是测试这个手势识别器。以下代码由测试记录器创建,但在刚测试时不起作用。

    let app = XCUIApplication()
    let elem = app.otherElements.containingType(.Image, identifier:"elementName").element
    elem.tap()
    elem.tap()
    elem.tap()
    elem.tap()
    elem.tap()
    elem.tap()

我甚至尝试在每个元素之间添加一点延迟(关于我尝试过1.0到0.02秒之间的值),但似乎没有任何实际工作。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

Here is what I've found so far. At its fastest, a tap takes a little over one second in the UI tester, which is WAY too long (not even close by a wide margin). So I had to make a workaround (aka: a hack)

Here is my work-around.

  1. IN the UITests target I added an "Other Swift Flags" "-D TEST". Now from my code I can use preprocessor checks in my main project. '#if TEST'.
  2. I added a button to the ui on the view in question. Wired it via a standard "touch up inside" event handler.
  3. In my view controller I did something like this:

    #if !TEST

    myButton.hidden = true

    #endif

  4. Modified my UI test code to find that button and do a simple tap instead.