简体   繁体   中英

Testing TapGesture with 6 repeats (iOS and Swift)

This might be a fun one. I'm setting up UI testing in XCode. One part of my app requires the user to tap on the screen 6 times to perform a particular action. The code is below.

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

Where I am having trouble is testing this gesture recognizer. The following code is created by the test recorder, but doesn't work when just tested.

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

I even tried adding a slight delay between each element (about I've tried values between 1.0 and 0.02 seconds), but nothing seems to actually work.

Any thoughts?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM