如何在 UITests 的 `tableViewCell` 中访问 `detailTextLabel`?

How to access the `detailTextLabel` in a `tableViewCell` at UITests?

我想检查我的 UITest 中是否有 tableViewCell.detailTextLabel 给定的字符串。问题是当我搜索 app.tables.cells.children(matching: .staticText) 时,它只会查找 tableViewCell.textLabel 的标签。 关于如何查询 detailTextLabel?

的任何想法

您可以尝试更通用的查询来确定文本是否存在。例如,如果您试图断言某个单元格包含“123 Main St.”,您可以使用以下内容:

let app = XCUIApplication()
XCTAssert(app.staticTexts["123 Main St."]).exists)

当然,如果该文本出现在 textLabel 中,测试仍然会通过。

听起来您的详细信息文本标签无法访问。

要查看视图层次结构中可用于测试的所有内容,请打印以下查询的调试说明,这可能有助于您了解元素是否出现、其类型是什么以及其标识符或标签是什么是:

let app = XCUIApplication()
print(app.descendants(matching: .any).debugDescription)

确保包含视图(单元格?)不可访问,并且详细文本标签可访问,并为其指定一个标识符:

let cell = UITableViewCell!
cell.isAccessibilityElement = false
cell.detailTextLabel.isAccessibilityElement = true
cell.detailTextLabel.accessibilityIdentifier = "myDetailTextLabel"