Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong cells count for collection view in UI Tests

I have a test for a collection view that works like this:

func testDeleteItem() {
    app.collectionViews.staticTexts["Item"].tap()
    app.buttons["Delete"].tap()

    XCTAssertEqual(app.collectionViews.cells.count, 2)
    XCTAssertFalse(app.collectionViews.cells.staticTexts["Item"].exists)
}

After the tap, there is a new screen with the delete button. When the button is tapped, the screen dismisses itself and reloads the collection view. Everything goes as expected in the UI, but I get both asserts failing. In the first count it is still 3 and in the second item it still exists.

like image 958
Tomasz Bąk Avatar asked Nov 12 '15 19:11

Tomasz Bąk


1 Answers

I have found the solution, but it's a workaround for wrong API behavior. Collection view is caching cells, that's probably why I have 3 cells, even if I have removed one. Deleted cell is offscreen, so you can test if it is hittable:

XCTAssertFalse(app.cells.staticTexts["Item"].hittable)

To find a count, I have created extension:

extension XCUIElementQuery {
    var countForHittables: UInt {
        return UInt(allElementsBoundByIndex.filter { $0.hittable }.count)
    }
}

and my test looks like this:

func testDeleteItem() {
    app.collectionViews.staticTexts["Item"].tap()
    app.buttons["Delete"].tap()

    XCTAssertEqual(app.collectionViews.cells.countForHittables, 2)
    XCTAssertFalse(app.collectionViews.cells.staticTexts["Item"].hittable)
}
like image 152
Tomasz Bąk Avatar answered Sep 22 '22 22:09

Tomasz Bąk