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
  • 86,719
  • 67
  • 308
  • 465
YogevSitton
  • 10,068
  • 11
  • 62
  • 95

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č
  • 31,030
  • 13
  • 103
  • 118
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
  • 9,289
  • 12
  • 69
  • 108
Aaron Sofaer
  • 706
  • 4
  • 19