代码之家  ›  专栏  ›  技术社区  ›  SleepNot

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

  •  0
  • SleepNot  · 技术社区  · 7 年前

    UIView 有一个 UITableView 我已经为 UIView公司 以及 . 但是,我只能查询 UIView公司 在我的UITests上,表视图根本不显示。我还设置了 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 ]))
      }
    

    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)
    
    0 回复  |  直到 7 年前
        1
  •  0
  •   cesarmarch    7 年前

    你试过用 filterView.tables 而不是 filterView.otherElements

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