Xcode UI test : Accessibility query fail on UITableViewCell
Asked Answered
P

2

6

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

Pontiac answered 5/9, 2016 at 11:21 Comment(0)
P
4

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"
Photoemission answered 5/9, 2016 at 12:59 Comment(2)
Thank you for posting this! I found it helpful for creating screenshots with fastlane.Curch
Where are these lines located?Hoban
C
0

In your example project, you are looking for a table within a table.

let tableView = XCUIApplication().tables.containingType(.Table, identifier: "Thetable")

You should use matchingIdentifier:, which searches through the tables on the screen, instead of containingType:identifier:, which searches through the descendants of the tables on the screen.

Crampton answered 6/9, 2016 at 13:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.