I am trying to create a test case in XCTest using the UI Test target. I have a use case where I use a SwiftUI Canvas to draw a network graph; and the user can drag the graph on the Canvas. After the drag, the user may click on a node. The application presents a popup with the information about the network node clicked. SwiftUI provides the information required for the application to translate the location of the tap into the original location prior to the drag; and so the logic behind the popup can determine which node the network graph was tapped by the user.
It appears that the distance of the drag operation performed does not match distance that the image actually moved. The drag distance is always larger than the actual distance dragged.
I can observe this during the test execution. The arrow starts at the correct spot but then goes further than the object being dragged. I also observe this when I am driving the application.
Is there some standard and supported way to determine the distance that the image on the screen was moved given a specific distance of the "thenDragTo:" coordinate?
I built a test project with a SwiftUI Canvas that draws a simple
network graph and pans via a DragGesture. I added UI tests that use
XCUICoordinate.press(forDuration:thenDragTo:) with various drag
distances and compared the requested distance against the content
offset the app reported. In every case the values matched 1:1 — a
200-point drag moved the content exactly 200 points.
This suggests the mismatch you're seeing is specific to your view hierarchy or how you apply the drag translation. A few things to check:
-
Scale transforms: If you apply a scale to the Canvas or a parent view (for example, pinch-to-zoom), the gesture translation arrives in screen points while the content moves in scaled coordinates. A 2x scale would make the content appear to move twice as far as expected.
-
Coordinate space: If your DragGesture specifies a
coordinateSpaceother than.local, the translation values are reported in that space, which may not match the Canvas coordinate system. -
Nested scroll views or gesture-intercepting containers: A ScrollView or other container with its own gesture handling can add translation on top of what your DragGesture reports.
-
CGAffineTransform vs offset: If you apply the drag as a transform rather than a direct offset, transforms can compound in ways that amplify movement.
You mentioned you also observe this when driving the application manually, which confirms the issue is in how the drag translation maps to content movement — not specific to XCUITest.
Could you share more about your view hierarchy and how you apply the drag translation to the Canvas content? That would help narrow down the cause.