Kelvin Tan
Kelvin Tan

Reputation: 992

Swift UI test, how to check if a cell has an imageview

In UI test, I can get the first cell using this code:

let app = XCUIApplication()

app.launch()

let tablesQuery = app.tables

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

How to check if that cell contains a imageview ?

Upvotes: 3

Views: 2536

Answers (5)

AntiVIRUZ
AntiVIRUZ

Reputation: 342

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

Instead of .assert(\.exists) you can do what you want with this XCUIElement of image view

Upvotes: 0

froggomad
froggomad

Reputation: 1905

Swift 5

Give the cell's ImageView an accessibility identifier first either in storyboard or ViewDidLoad

Storyboard Accessibility ID

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

Upvotes: 2

Suhit Patil
Suhit Patil

Reputation: 12023

  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")
      }
  }

Upvotes: -1

Pavankumar
Pavankumar

Reputation: 288

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

Upvotes: 0

sdasdadas
sdasdadas

Reputation: 25096

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

Upvotes: 1

Related Questions