Skip to content

Instantly share code, notes, and snippets.

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 :(")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.