Swift UI测试,如何检查单元格是否有imageview

k3bvogb1  于 2023-04-19  发布在  Swift
关注(0)|答案(5)|浏览(113)

在UI测试中,我可以使用以下代码获取第一个单元格:

let app = XCUIApplication()

app.launch()

let tablesQuery = app.tables

let cell = tablesQuery.children(matching:.any).element(boundBy: 0)

如何检查该单元格是否包含imageview?

uxh89sit

uxh89sit1#

Swift 5

首先在故事板或ViewDidLoad中为单元格的ImageView提供一个可访问性标识符

func testIsImageViewNil() {
    let imageView = app.images["PhotosCollectionViewController.ImageCell.ImageView"]
    XCTAssertNotNil(imageView)
}
6ju8rftf

6ju8rftf2#

public func hasImageViewInside(_ cell: UITableViewCell) -> Bool {
    for child in cell.subviews {
        if let _ = child as? UIImageView {
            return true
        }
    }
    return false
}
4jb9z9bj

4jb9z9bj3#

for viw in cell.contentView.subviews {
        if ((viw as? UIImageView) != nil) {
            print("123")
        }
    }
sdnqo3pr

sdnqo3pr4#

cell.images.element.assert(\.exists)

您可以使用图像视图的这个XCUIElement来代替.assert(\.exists)

v440hwme

v440hwme5#

for case let imageView as UIImageView in cell.contentView.subviews  {
      if imageView.tag == 1001 {
         imageView.image = UIImage(named: "myCustomImage")
      }
  }

  //OR Altervnatively

  cell.contentView.subviews.flatMap { $0 as? UIImageView }.forEach { imageView in
      if imageView.tag == 1001 {
         imageView.image = UIImage(named: "myCustomImage")
      }
  }

相关问题