How can I verify existence of text inside a table view row given its index in an XCTest UI Test?
Asked Answered
P

2

6

I am using XCTest UI Test framework to check rows in a table view. In my test I need to verify the existence of text in a table view row given its index.

I was thinking of using tableRows.elementBoundByIndex method, but it did not return anything. The number of rows returned by calling tableRows.count is zero. The table is present and filled because I can query the text inside the rows successfully:

XCTAssertEqual(app.tables.staticTexts["Cute Alpaca"].exists)

How can I verify existence of a text in a row given its index in an XCTest UI test?

Proline answered 3/9, 2015 at 0:46 Comment(0)
S
18

It turns out that tableRows doesn't actually detect rows in the UITableView. Use cells instead.

I have a feeling that tableRows and tableColumns are used to do testing on Mac OS X where you have actual tables while on iOS we have UITableViewCell and UICollectionViewCell. Or this could be a bug as we are still in the beta period.

let table = app.tables.element
XCTAssertTrue(table.exists)

let cell = table.cells.elementBoundByIndex(2)
XCTAssertTrue(cell.exists)
let indexedText = cell.staticTexts.element
XCTAssertTrue(indexedText.exists)

or the one liner

XCTAssertTrue(app.tables.element.cells.elementBoundByIndex(2).exists)
Scherman answered 4/9, 2015 at 2:8 Comment(0)
M
1

For your case, I think this code will help you.

let tableCellContainer = app.tables["HomeTableView"].cells.element(boundBy: 1) // Get the first Cell
let cell = tableCellContainer.staticTexts["Brazil"]
XCTAssert(cell.exists)

Regards,

Mawkin answered 6/11, 2017 at 3:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.