首页 > 解决方案 > 自定义 UIView 的子视图未出现在 UI 测试中

问题描述

我有UIView一个UITableView作为子视图的自定义。我已经为 和 设置了accessibilityLabel 和UIViewaccessibilityIdentifier UITableView。但是,我只能UIView在我的 UITests 上查询,并且表格视图根本不会出现。我也设置isAccessibilityElementtrue.

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

我正在访问我的 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)

标签: iosswiftxcuitest

解决方案


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

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


推荐阅读