iosswiftuixcode-ui-testinggui-testing

How to UITest a cell' value of ListView in SwiftUI?


I have a note taking app. I have added 4 notes (Note 1, Note 2, Note 3, Note 4) as a part of app's UITest case. Now I would like to test few edge cases which are:

  1. The ListView has exactly 4 cells.

  2. Note 2 is on second cell.

     func test_notesVew_addNewMultipleNote_shouldSeeNewNotesInHomeScreen() {
        let addNewButton = app.navigationBars["Notes"].buttons["Add new note button"]
        addNewButton.tap()
    
        let collectionViewsQuery = app.collectionViews
    
        createNote(num: 1)
        addNewButton.tap()
        createNote(num: 2)
        addNewButton.tap()
        createNote(num: 3)
        addNewButton.tap()
        createNote(num: 4)
    
        // MARK: - Scenario: Validating added notes
        let cellCount = collectionViewsQuery.cells.count
        XCTAssertEqual(cellCount, 4, "After adding 4 notes, cell count should be 4")
    
        // MARK: - Scenario: Validating note's order
        //let noteOnSecondRow = collectionViewsQuery.element(boundBy: 1)
     }
    

How to access value or index of a specific cell of a ListView?


Solution

  • Your commented solution is not looking for the second cell, it's looking for the second collectionView. The following should get you what you need:

    let secondCell = collectionViewsQuery.cells.element(boundBy: 1)
    let secondCellValue = secondCell.staticTexts.element.label