【问题标题】:Subview of a custom UIView not appearing in UI Tests自定义 UIView 的子视图未出现在 UI 测试中
【发布时间】:2019-05-24 01:35:16
【问题描述】:

我有一个自定义 UIView,它有一个 UITableView 作为子视图。 我已经为UIViewUITableView 设置了accessibilityLabel 和accessibilityIdentifier。但是,我只能在我的 UITests 上查询UIView,并且表格视图根本不会出现。我还将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 ]))
  }

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

【问题讨论】:

  • 我不知道您的 tableView 是如何初始化的。在您通过标识符访问它之前,您的 tableView 是否已初始化?

标签: ios swift xcuitest


【解决方案1】:

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

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

【讨论】:

  • 您是否尝试通过添加断点并使用调试器检查来查找表视图?
猜你喜欢
  • 2013-11-19
  • 1970-01-01
  • 1970-01-01
  • 2015-11-22
  • 2016-01-27
  • 2021-03-16
  • 1970-01-01
  • 1970-01-01
  • 2016-07-29
相关资源
最近更新 更多