【问题标题】:Swift UI test, how to check if a cell has an imageviewSwift UI 测试,如何检查单元格是否有图像视图
【发布时间】:2017-01-24 12:20:19
【问题描述】:

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

let app = XCUIApplication()

app.launch()

let tablesQuery = app.tables

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

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

【问题讨论】:

    标签: ios swift xcode unit-testing uitest


    【解决方案1】:
    public func hasImageViewInside(_ cell: UITableViewCell) -> Bool {
        for child in cell.subviews {
            if let _ = child as? UIImageView {
                return true
            }
        }
        return false
    }
    

    【讨论】:

    • 如果 imageView 的深度超过一个级别(它可能是)将不起作用,前提是它已正确添加到 cell.contentView
    • @ConnorNeville 你是对的,但让它递归并不难。这个想法是一样的。
    • 在 UI 测试中查询时您无权访问它吗?
    【解决方案2】:
            for viw in cell.contentView.subviews {
            if ((viw as? UIImageView) != nil) {
                print("123")
            }
        }
    

    【讨论】:

      【解决方案3】:

      斯威夫特 5

      首先在情节提要或ViewDidLoad 中为单元格的 ImageView 提供一个可访问性标识符

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

      【讨论】:

        【解决方案4】:
          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")
              }
          }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多