So you want to find a tableView's cell and then tap on it.
This solution is posted after testing in Xcode 9.0 beta 6, using Swift 4
As mentioned by Oletha in a comment, it's a good idea to understand the hierarchy of your view by printing the debugDescription:
let app = XCUIApplication()
override func setUp() {
super.setUp()
continueAfterFailure = false
app.launch()
print(app.debugDescription)
}
The print statement will give you a detailed list of views and their hierarchy.
Next, you should add an accessibilityIdentifier to your tableview and cell as follows:
a) Inside your viewDidLoad() or any other relevant function of the controller-
myTableView.accessibilityIdentifier = “myUniqueTableViewIdentifier”
b) Inside your tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) function-
cell.accessibilityIdentifier = "myCell_\(indexPath.row)"
Once done, you can write the following code in your test case to get the desired results:
let myTable = app.tables.matching(identifier: "myUniqueTableViewIdentifier")
let cell = myTable.cells.element(matching: .cell, identifier: "myCell_0")
cell.tap()
Note that this will simply help you find and tap the required cell, you can add some XCTAssert checks inside your test case to make it more useful.
P.S.: There are many guides online which can help you learn the UI testing with Xcode. e.g.: https://blog.metova.com/guide-xcode-ui-test/
XCUIApplication().tables.cells.debugDescription
so we can see what we are working with. – Schach