如何使用 UI 测试用例验证 UITableviewCell 标签和 Imageview 数据

How to validate UITableviewCell label and Imageview data with UI Test Cases

我有一个 UITableView,其中包含一些来自 Web 服务的数据。它在每个单元格中都有多个 UILabels 和 UIImageView。如何从单元格中获取标签和 UIImageView 并验证它们是否具有有效数据?我只想测试第一个单元格

我能够如下获取第一个单元格实例,但无法获取其子元素。

我获取第一个单元格的代码:

let app = XCUIApplication()
        let myTable = app.tables.matching(identifier: "property_list_tableview")
        let cell = myTable.cells.element(matching: .cell, identifier: "cell_0")

现在我想从单元格中提取标签和图像视图。我已经为标签和图像视图添加了可访问性标识符。

您可以选择将辅助功能标签添加为 UI 标识符,然后您可以轻松 select 它并获取它的值。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
  ...
  let = tableView.dequeueReusableCell(withIdentifier: "CustomCell")! as! CustomCell

  cell.yourUIImage.image?.accessibilityLabel = "image " + String(indexPath.row)

}

然后在 UITests.swift

func testAreImagesLoaded() {
    let image = "image "

    XCTAssert(XCUIApplication().images[image + String(0)].exists)
    XCTAssert(XCUIApplication().images[image + String(1)].exists)
    XCTAssert(XCUIApplication().images[image + String(2)].exists)
}

您可以访问字符串示例的值

let labelElement = XCUIApplication().staticTexts["myLabel"]
...
XCTAssertEqual(labelElement.value as! String, "your text")

但不要忘记为 UILabel

添加辅助功能标签

查看此 link 了解更多信息 https://medium.com/flawless-app-stories/automated-ui-testing-in-swift-ios-46e1c9993316

使用以下内容比较标签值的文本

let app = XCUIApplication()
let myTable = app.tables["property_list_tableview"]
XCTAssertEqual(myTable.cells["cell_0"].staticTexts["field_name"].label, "Text to compare")