Skip to content

Instantly share code, notes, and snippets.

Last active June 10, 2022 14:25
  • Star 18 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
XCUITests: scroll UICollectionView to find one of it's offscreen UICollectionViewCell by id
// Thanks to @MonsieurDart for the idea :)
func scroll(collectionView:XCUIElement, toFindCellWithId identifier:String) -> XCUIElement? {
guard collectionView.elementType == .collectionView else {
fatalError("XCUIElement is not a collectionView.")
var reachedTheEnd = false
var allVisibleElements = [String]()
while !reachedTheEnd {
let cell = collectionView.cells[identifier]
// Did we find our cell ?
if cell.exists {
return cell
// If not: we store the list of all the elements we've got in the CollectionView
let allElements ={$0.identifier})
// Did we read then end of the CollectionView ?
// i.e: do we have the same elements visible than before scrolling ?
reachedTheEnd = (allElements == allVisibleElements)
allVisibleElements = allElements
// Then, we do a scroll up on the scrollview
let startCoordinate = collectionView.coordinate(withNormalizedOffset: CGVector(dx: 0.99, dy: 0.9)) 0.01, thenDragTo: collectionView.coordinate(withNormalizedOffset:CGVector(dx: 0.99, dy: 0.1)))
return nil
// After this, you may want to scroll to top ...
func statusBarScrollToTop() {
let statusBar = XCUIApplication().statusBars.element
// Sample usage:
if let cell = scroll(collectionView: emojislistCollectionView, toFindCellWithId: "myID") {
} else {
XCTFail("Unable to find the cell :(")
Copy link

hey, the statusBar definition in statusBarScrollToTop no longer works

it needs to be changed to

let statusBar = XCUIApplication(bundleIdentifier: "").statusBars.element(boundBy: 1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment