Not precise scroll in XCTest

I'm working on UI automation tests using XCUITest for an iOS application (iPhone). My goal is to programmatically scroll a view by a very precise number of pixels (e.g., exactly 500 points down). I understand the scroll(byDeltaX:deltaY:) method is not supported on iPhone, so I'm using the coordinate-based drag method as an alternative.

Specifically, I am using XCUICoordinate.press(forDuration:thenDragTo:withVelocity:thenHoldForDuration:) to simulate a drag gesture. I calculate a start and end coordinate with a specific vertical offset in points, expecting the view to scroll by that exact amount.

However, I'm observing that the resulting scroll offset is not perfectly accurate. There's a consistent error of several pixels, making the scroll amount unpredictable for precise test assertions.

Is there a known limitation to the accuracy of coordinate-based dragging for simulating programmatic scrolling? Are there any alternative methods or best practices within XCUITest to achieve a more reliable and pixel-accurate scroll on iPhone, or is this level of precision simply not achievable with the current framework?

Hello IvanPushkov,

Thanks for your question. XCUICoordinate.press(forDuration:thenDragTo:withVelocity:thenHoldForDuration:) is indeed a recommendable way to simulate scrolling in iOS XCUITests. Could you possible create a focused test project that replicates the issues in calculating accurate offsets, so we can troubleshoot it? If so, please share a link to your test project.

If you're not familiar with preparing a test project, take a look at Creating a test project.

Thank you,

Richard Yeh  Developer Technical Support

Not precise scroll in XCTest
 
 
Q