3

I want to use Xcode's UI Testing to count the number of sections in a tableview and the number of cells in each section. How can I do that?

Senseful
  • 79,995
  • 61
  • 289
  • 423
YogevSitton
  • 9,908
  • 11
  • 59
  • 93

2 Answers2

4

As of Xcode 7, table view headers show as Other elements.

Here's what I did for a (grouped) table view in my app:

extension TableLayoutTests {

    func testHasMessagesGroup() {
        XCTAssert(app.tables.otherElements["MESSAGES"].exists)
    }

    func testHasMessageCell() {
        let header = app.tables.otherElements["MESSAGES"]
        let cell = app.tables.cells.elementBoundByIndex(1)
        XCTAssertGreaterThanOrEqual(cell.accessibilityFrame.minY, header.accessibilityFrame.maxY)
    }

    func testHasOtherMessageCell() {
        let header = app.tables.otherElements["MESSAGES"]
        let cell = app.tables.cells.elementBoundByIndex(2)
        XCTAssertGreaterThanOrEqual(cell.accessibilityFrame.minY, header.accessibilityFrame.maxY)
    }

}
Rudolf Adamkovič
  • 30,008
  • 11
  • 100
  • 116
2

Joe Masilotti is entirely correct in that application code cannot be accessed directly from the UI Test, though you can backchannel if you want to be out-of-style. Let's assume you don't.

Xcode's UI Testing framework has access to all of the Swift language along with the UI hierarchy of your app, but not access to the data layer; so you can do the following:

  • Give the headers or footers of the tableview sections accessibilityIdentifiers that include an index component (e.g., photos.headers_0 , etc)
  • Having done that, use conditionals to determine the number of sections
  • Similarly, photos.section_0.cell_0 can be assigned in your data source, and an iterator + conditional can store the number of cells in section 0 or any other section.
shim
  • 8,356
  • 10
  • 70
  • 102
Aaron Sofaer
  • 668
  • 4
  • 19