Search code examples
iosobjective-cxctest

XCTest - UI Testing with XCUIElement using pinch gesture on custom coordinates?


Let's say I've an app with an UI which looks kind of like the image below:

UI to test

Let's also say the blue view behaves like a map and the other colors are other interactable views.

I need to do a XCTest where I need to "zoom out" on that blue view which I tired with pinchWithScale:

[blueView pinchWithScale:0.5 velocity:-1];

Unfortunately that doesn't work because one of the red views or the violet view (which overlaps the blue view a tiny bit for shadow and corner reasons) gets triggered instead of a pinch gesture on the blue view.

I saw that I can get coordinateWithNormalizedOffset (similar like here) of a view by using the method:

- (XCUICoordinate *)coordinateWithNormalizedOffset:(CGVector)normalizedOffset;

which would allow me to use not the whole blue view to perform a "zoom out" gesture by:

XCUICoordinate* blueViewInset = [blueView coordinateWithNormalizedOffset:
                                 CGVectorMake(0.2f, 0.2f)];

but the XCUICoordinate object blueViewInset doesn't support pinch gestures.

Hence how can I perform a pinch gesture to "zoom out" on a XCUIElement with coordinates not at the border of the view?

I'm also wondering if there is a way to create an extension to XCUIElement with a custom pinch gesture? I would appreciate any hints on that too.


Solution

  • I figured out that I can (but don't want to) add another empty view on top of my blueView with constraints to half of the size of it (see yellowish rectangle in the illustration below). This works for all my cases so far because no other view overlaps that far. It looks something like this:

    UI with emtpy view for pinch gestures

    Executing a pinchWithScale on that empty view works and actually pinches the blueView.

    This is a very ugly solution, since I've to add a view which is solely for UI testing.

    If anyone has a hint on how to remove it for the release build let me know.

    Update

    I made a build step running a script which adds this view only when executing the UI automation test target. After the execution the source control (e.g. git reset HEAD~) is used to undo the added view.