【问题标题】:Xcode UI test : Accessibility query fail on UITableViewCellXcode UI 测试:UITableViewCell 上的辅助功能查询失败
【发布时间】:2016-09-05 11:21:24
【问题描述】:

问题

使用 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

【问题讨论】:

    标签: swift xcode uitableview xcode-ui-testing


    【解决方案1】:

    我找到了答案here。为了使UITableViewCell 可访问,包含UITableView 本身不能访问。

    所以,你只需要删除这些行:

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

    【讨论】:

    • 感谢您发布此信息!我发现使用fastlane 创建屏幕截图很有帮助。
    • 这些行在哪里?
    【解决方案2】:

    在您的示例项目中,您正在查找表中的表。

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

    您应该使用matchingIdentifier:,它搜索屏幕上的表格,而不是containingType:identifier:,它搜索屏幕上表格的后代

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-05
      • 2017-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-02
      相关资源
      最近更新 更多