简体   繁体   English

自定义UIView的子视图未出现在UI测试中

[英]Subview of a custom UIView not appearing in UI Tests

I have a custom UIView that has a UITableView as a subview. 我有一个自定义UIView ,它有一个UITableView作为子视图。 I have set the accessibilityLabel and accessibilityIdentifier for both the UIView and the UITableView . 我为UIViewUITableView设置了accessibilityLabel和accessibilityIdentifier。 However, I can only query the UIView on my UITests and the table view does not appear at all. 但是,我只能在我的UITests上查询UIView ,并且表格视图根本不显示。 I have also set the isAccessibilityElement to true . 我还将isAccessibilityElement设置为true

  public lazy var tableView: UITableView = {
    let tableView = UITableView()
    tableView.dataSource = self
    tableView.delegate = self
    tableView.translatesAutoresizingMaskIntoConstraints = false
    tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
    return tableView
  }()

  // MARK: - Initialization

  override init(frame: CGRect) {
    super.init(frame: .zero)
    commonInit()
    self.layer.borderColor = UIColor.lightGray.cgColor
    self.layer.borderWidth = 1
  }

  required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    commonInit()
  }

  // MARK: - Private Methods

  private func commonInit() {
    isAccessibilityElement = true
    accessibilityLabel = "Filter View"

    tableView.isAccessibilityElement = true
    tableView.accessibilityLabel = "Filter View Table"
    tableView.accessibilityIdentifier = "Filter View Table"

    addSubview(tableView)
    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[tableView]-0-|", options: .alignAllLeft, metrics: nil, views: [ "tableView": tableView ]))
    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[tableView]-0-|", options: .alignAllLeft, metrics: nil, views: [ "tableView": tableView ]))
  }

I am accessing the controls in my UITests like: 我正在访问我的UITests中的控件,如:

let filterView = app.otherElements["Filter View"] // Custom View
XCTAssertTrue(filterView.exists)
let filterTableView = filterView.otherElements["Filter View Table"] // Table View as Custom View's subview
XCTAssertTrue(filterTableView.exists)

您是否尝试过使用filterView.tables而不是filterView.otherElements

let filterTableView = filterView.tables["Filter View Table"] // Table View as Custom View's subview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM