XCUI scroll to element

Has anybody found a good way to scroll to an element?


I can do something like the below function. But I am running into situation where a single swipeUp is too much swipe!! It looks like swipeUp is about 1.5 the size in view. So when I swipe up the element I am wanting goes out of view in the top.


Any ideas?


Thanks!


func swipeUpUntilElementFound(app: XCUIApplication, element : XCUIElement, maxNumberOfSwipes : UInt) -> Bool{
        if element.exists{
            return true
        }
        else {
            for _ in 1...maxNumberOfSwipes{
                app.swipeUp()
                if element.exists{
                    return true
                }
            }
            return false
        }
    }
  • Looks like this issue is still valid for desktop apps. My tests failed because it doesn't want to scroll automatically to find a needed element.

Add a Comment

Accepted Reply

You are not supposed to have to scroll manually. Interacting with an element that is not currently visible in the scroll view is expected to implicitly first scroll the element to be visible, without requiring effort on your part. If that's not working for you, please file a bug report (using the Report Bugs link at the bottom of this page). Thanks!

Replies

You are not supposed to have to scroll manually. Interacting with an element that is not currently visible in the scroll view is expected to implicitly first scroll the element to be visible, without requiring effort on your part. If that's not working for you, please file a bug report (using the Report Bugs link at the bottom of this page). Thanks!

Like Joar said, it's supposed to do it automagically. That broke in beta 6, though, and it's still broken in the GM. Radar 22584968, and a similar thread from someone else here: https://forums.developer.apple.com/message/47116#47116

thanks for the info nick

Hey Nick have you tried 7.1 yet?

I just tried in XCode 7.1 beta and it is still broke 😢

Would you mind posting your bug report to Open Radar so the community can duplicate it?

Tried this in Xcode 9 and it doesn't work.

One solution I like is to scroll half of the scrollable element


let relativeTouchPoint = coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.5))

let relativeOffset = coordinate(withNormalizedOffset: CGVector(dx: 0, dy: -1))

relativeTouchPoint.press(forDuration: 0, thenDragTo: relativeOffset)


The above code will scroll by swiping upward to see content down below. It should be about half the scroll view height.

Repeat as neccessary.

Thats great that xcode should handle the scroll to the element for you however I am still finding that it doesn't always do this. One day it will scroll to the element just fine and then another it won't and my tests fail. This is two years later. I plan to file a bug report on this but It would be nice to know the best way to have a safe guard there so my tests aren't so flaky.

I tried this feature in tableview and it doesnt work, all I get is an "Element not found error", it doesnt scroll automatically

XCode 9.4 and iOS 11.4

XCode 12.5.1 - ios 14.5 - it gives you access to the first 3 items, but you have to scroll further for it to see beyond those 3...but it does remember the first three items....so you can "acquire" the list of items by scrolling through it