Xcode UI测试: UITableViewCell无法进行可访问性查询

6

问题

使用Xcode UI测试时,我无法查询UITableView中的单元格。

解释

UITableView

UITableView包含3个单元格:

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
    }
}

测试

这个测试非常简单。

假设有一个包含3个单元格的UITableView,我想要验证是否有任何单元格可用:

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

它会在2个断言上失败:
Assertion Failure: XCTAssertTrue failed - 
/Users/damiengavard/Desktop/Table/TableUITests/TableUITests.swift:33: error: -[TableUITests.TableUITests testExample] : XCTAssertTrue failed - 

如何重现

https://github.com/dagio/TableCellAccessibility

只需执行Cmd+U即可。

2个回答

4
我在这里找到了答案 (链接)。为了使UITableViewCell可以访问,其包含的UITableView不能本身是可访问的。
所以,你只需要删除这些行:
tableView.isAccessibilityElement = true
tableView.accessibilityLabel = "Thetable"
tableView.accessibilityIdentifier = "Thetable"

1
谢谢您发布这篇文章!我发现它对使用 fastlane 创建截图非常有帮助。 - Adrian
1
这些代码行位于哪里? - Cory McAboy

0
在您的示例项目中,您正在寻找一个表格中的另一个表格。
let tableView = XCUIApplication().tables.containingType(.Table, identifier: "Thetable")

你应该使用matchingIdentifier:,它可以在屏幕上搜索表格,而不是containingType:identifier:,它只能在屏幕上的表格后代中搜索。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接