1

I work with swift's UI testing, and I need to be able to tap a specific point on my app. I followed the answers from How to tap on a specific point using Xcode UITests, which worked great for testing on iPad. However, when doing the same on the simulator nothing happens. No errors, the program simply acts like I'm pressing something, but not anything in particular, and the program can't continue executing since the right info isn't there.

Is this possibly a local problem? If not, what can I do to tap at specific coordinates on the simulator?

Parakit
  • 11
  • 2
  • 3
    (1) I believe the short answer is - you can't. It's a simulator! To "tap" you "click". How do you tap on a specific point on a real device? Short answer? You don't. You *record* the specific point that *was* tapped. Same with the simulator. Anything otherwise is illogical. (2) That said, what *exactly* are you trying to do? Test something? As it is, your question is somewhat vague. If all you wish to do is test some code where a specific point is "tapped" - which is all you've said - then "hard-code" it! –  Nov 07 '18 at 09:54

1 Answers1

0

Well, you cannot just tap a screen. You have to tap on something.

Easiest thing to do would be tapping using elements in app and method coordinate(withNormalizedOffset:) from Apple.

Create a method, thats gonna have input parameters element (you want to tap on) and offset (the point, you want to tap on). Something like this:

func tap(on element: XCUIElement, xOffset: Double, yOffset: Double) {
    let coordinate = element.coordinate(withNormalizedOffset: CGVector(dx: xOffset, dy: yOffset))

    if element.waitForExistence(5) {
         coordinate.tap()
    }
}

You can then tap on any element within the tested app by calling this method and giving it the element.

F.e.: You want to tap on some point in app itself with 0.8 and 0.5 offset:

tap(on: XCUIApplication(), xOffset: 0.8, yOffset: 0.5)

Václav
  • 990
  • 1
  • 12
  • 28