YogevSitton
YogevSitton

Reputation: 10108

Xcode UI Testing - UITableView sections

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?

Upvotes: 3

Views: 4522

Answers (2)

Aaron Sofaer
Aaron Sofaer

Reputation: 726

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.

Upvotes: 2

Rudolf Adamkovič
Rudolf Adamkovič

Reputation: 31486

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)
    }

}

Upvotes: 4

Related Questions