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?
Asked
Active
Viewed 4,398 times
3

Senseful
- 86,719
- 67
- 308
- 465

YogevSitton
- 10,068
- 11
- 62
- 95
-
Why not to use your datasource instead ? – Mihir Mehta Dec 17 '15 at 08:50
-
1. How can I access code from a UI test? 2. I want to make sure the sections are parsed correctly from the data source – YogevSitton Dec 17 '15 at 08:59
-
just call tableview's numberOfSection and numberOfRows from your UI test class ... ?(Not sure if i am getting your question right) – Mihir Mehta Dec 17 '15 at 09:04
-
I don't have access to the tableview from the UI test class – YogevSitton Dec 17 '15 at 09:08
-
is it restricted or you don't know how to make it accessible ? – Mihir Mehta Dec 17 '15 at 09:13
-
I don't know if it's even possible – YogevSitton Dec 17 '15 at 10:48
-
it's very much possible i think – Mihir Mehta Dec 17 '15 at 11:07
-
1It's not possible to access the application code directly from the UI Testing target. See [this answer](http://stackoverflow.com/a/32076413/384110) for more details. – Joe Masilotti Feb 28 '16 at 17:34
2 Answers
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