2

I have a collectionview that has 8 cells. Each cell has a label that contains some different text. I want to test if each cell contains a label with the correct text. I'm using a loop to go through each text and asset if a cell containing that text exists.

The problem is that the 7th & 8th cells (indices 6 & 7) are off-screen so the test fails.

let names = ["First", "Second", "Third", "Fourth", "Fifth", "Sixth", "Seventh", "Eighth"]
for name in names {
    let cellWithName = app.cells.containing(.staticText, identifier: name).element
    XCTAssert(cellWithName.exists)
}

enter image description here

I was given a solution: to restrict the names array to only the first 6 names, then create another array with the last 2 names and test them separately when on on-screen, but I can already imagine a whole host of problems with this solution.

Does anyone know a better solution to testing UI elements that are off-screen?

leedex
  • 963
  • 8
  • 19
  • You can count your cells first to make sure you have the correct amount, then select each one and interrogate it for the desired values. This has the benefit of being flexible across form factors as well. – CodeBender Jun 10 '19 at 18:23
  • 1
    I'm in the same situation that yours. In my case, I automatically scroll the CollectionView (either with `app.swipeUp()` or using `collectionView.press(forDuration: 0.0, thenDragTo: scrollVector)` ) to display next cells and keep a Set with all my cells' identifiers to be sure they are unique and then make assertions on the content of the Set. – cesarmarch Jun 14 '19 at 13:48

0 Answers0