Damien Gavard Damien Gavard - 3 months ago 34
Swift Question

Xcode UI test : Accessibility query fail on UITableViewCell

The issue



Using Xcode UI test, I can not query the cells in a UITableView

Explanations



The UITableView



The UITableView contains 3 cells :

import UIKit

@objc class DumpTable: UITableViewController {
var objects: [NSDate] = [NSDate]()

override func viewDidLoad() {
super.viewDidLoad()

objects.append(NSDate())
objects.append(NSDate())
objects.append(NSDate())

tableView.isAccessibilityElement = true
tableView.accessibilityLabel = "Thetable"
tableView.accessibilityIdentifier = "Thetable"
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return objects.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell()

let object = objects[indexPath.row]
cell.textLabel!.text = object.description

cell.isAccessibilityElement = true
cell.accessibilityLabel = "Thecell"
cell.accessibilityIdentifier = "Thecell"

return cell
}
}


The test



The test is really simple.

Given a UITableView with 3 cells, I'm trying to assert there are any cells available :

XCTAssertTrue(XCUIApplication().tables["Thetable"].exists)
XCTAssertTrue(XCUIApplication().tables["Thetable"].cells.count > 0)


It will then fail on the 2 assertions :

Assertion Failure: XCTAssertTrue failed -
/Users/damiengavard/Desktop/Table/TableUITests/TableUITests.swift:33: error: -[TableUITests.TableUITests testExample] : XCTAssertTrue failed -


How to reproduce



https://github.com/dagio/TableCellAccessibility

Simply execute Cmd+U

Answer

I found the answer here. In order to make the UITableViewCell accessible, the containing UITableView cannot be accessible itself.

So, you just need to remove these lines:

tableView.isAccessibilityElement = true
tableView.accessibilityLabel = "Thetable"
tableView.accessibilityIdentifier = "Thetable"