Search code examples
iosswiftxcodexctestuitest

Swift UITest Can not find collectionview inside of tableviewcell


I have a "collectionView" inside of a "tableViewCell" and at XCTest I can not access this "collectionview" or its "cells". here is my code to access this "collectionview". Also I used accessibilityIdentifier but there is nothing change. I can access "tableViewCell" but can not to "collectionView"

app.tables.cells.element(boundBy: 7).collectionViews.element(boundBy: 0).cells.element(boundBy: 0)

Solution

  • If I understand the question correctly, the reason you cannot get the collectionViewCell is because in the collectionViewCell, there are some gesture responders such as a button. If there is no such subView or some dumb one like a label, still you can get the collectionViewCells as the following:

      let collectionViewsQuery = XCUIApplication().tables.children(matching: .cell).element(boundBy: 1).collectionViews // the second table cell 
    
       let element = collectionViewsQuery.children(matching: .cell).element(boundBy: 3).children(matching: .other).element // the fourth cell
    
         let element2 = collectionViewsQuery.children(matching: .cell).element(boundBy: 4).children(matching: .other).element // the fifth cell , 
    

    etc ..

    However, if there is a button in any cell, you can not get the cell directly:

      let tablesQuery = XCUIApplication().tables
      let cell = tablesQuery.children(matching: .cell).element(boundBy: 1) // the second table cell
    
      let button = cell.children(matching: .button).element(boundBy: 0) // the button which lies at any of collectionCell and not defined.
    

    Hope this answers your question why not getting the collection cell. Actually, you can refer the cell first then try to refer the button later by two steps.