在UI测试中,我可以使用以下代码获取第一个单元格:
let app = XCUIApplication() app.launch() let tablesQuery = app.tables let cell = tablesQuery.children(matching:.any).element(boundBy: 0)
如何检查该单元格是否包含imageview?
uxh89sit1#
首先在故事板或ViewDidLoad中为单元格的ImageView提供一个可访问性标识符
ViewDidLoad
func testIsImageViewNil() { let imageView = app.images["PhotosCollectionViewController.ImageCell.ImageView"] XCTAssertNotNil(imageView) }
6ju8rftf2#
public func hasImageViewInside(_ cell: UITableViewCell) -> Bool { for child in cell.subviews { if let _ = child as? UIImageView { return true } } return false }
4jb9z9bj3#
for viw in cell.contentView.subviews { if ((viw as? UIImageView) != nil) { print("123") } }
sdnqo3pr4#
cell.images.element.assert(\.exists)
您可以使用图像视图的这个XCUIElement来代替.assert(\.exists)
.assert(\.exists)
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") } }
5条答案
按热度按时间uxh89sit1#
Swift 5
首先在故事板或
ViewDidLoad
中为单元格的ImageView提供一个可访问性标识符6ju8rftf2#
4jb9z9bj3#
sdnqo3pr4#
您可以使用图像视图的这个XCUIElement来代替
.assert(\.exists)
v440hwme5#