Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tap on a specific point using Xcode UITests

I want to use Xcode UI tests with the Fastlane Snapshot to make screenshots of the Cordova app. Basically, as my entire app is just a web view, all the Xcode UI test helper methods become irrelevant, and I just want to tap on specific points, e.g. tap(x: 10, y: 10) should produce a tap at the point {10px; 10px}.

That's probably very simple, but I can't figure out how to do it.

Thanks.

like image 872
Dmitry Sokurenko Avatar asked Mar 29 '16 13:03

Dmitry Sokurenko


1 Answers

You can tap a specific point with the XCUICoordinate API. Unfortunately you can't just say "tap 10,10" referencing a pixel coordinate. You will need to create the coordinate with a relative offset to an actual view.

We can use the mentioned web view to interact with the relative coordinate.

let app = XCUIApplication() let webView = app.webViews.element let coordinate = webView.coordinateWithNormalizedOffset(CGVector(dx: 10, dy: 10)) coordinate.tap() 

Side note, but have you tried interacting with the web view directly? I've had a lot of success using app.links["Link title"].tap() or app.staticTexts["A different link title"].tap(). Here's a demo app I put together demonstrating interacting with a web view.


Update: As Michal W. pointed out in the comments, you can now tap a coordinate directly, without worrying about normalizing the offset.

let normalized = webView.coordinate(withNormalizedOffset: CGVector(dx: 0, dy: 0)) let coordinate = normalized.withOffset(CGVector(dx: 10, dy: 10)) coordinate.tap() 

Notice that I pass 0,0 to the normalized vector and then the actual point, 10,10, to the second call.

like image 140
Joe Masilotti Avatar answered Sep 28 '22 01:09

Joe Masilotti