【问题标题】:How can I make UIControl appear as a button in XCUITest?如何使 UIControl 在 XCUITest 中显示为按钮?
【发布时间】:2016-12-09 11:46:20
【问题描述】:

我创建了一个 UIControl 的子类,它的行为应该类似于 UIButton。

当我使用 XCUITest 运行 UI 测试时,按钮出现在 XCUIApplication().staticTexts 而不是预期的 XCUIApplication().buttons

【问题讨论】:

    标签: ios xcode-ui-testing


    【解决方案1】:

    在四处寻找之后,我发现这归结为未设置可访问性特征。例如,如果我有以下课程;

    class ActivityButton: UIControl {
    
        private let activityIndicator: UIActivityIndicatorView = {
            let activityIndicator = UIActivityIndicatorView()
            activityIndicator.hidesWhenStopped = true
            activityIndicator.translatesAutoresizingMaskIntoConstraints = false
            return activityIndicator
        }()
    
        let titleLabel: UILabel = {
            let titleLabel = UILabel()
            titleLabel.translatesAutoresizingMaskIntoConstraints = false
            return titleLabel
        }()
    
        override public init(frame: CGRect) {
            super.init(frame: frame)
            // This is the required call to make the control appear 
            // as a button in ui tests
            accessibilityTraits = UIAccessibilityTraitButton
            addSubview(activityIndicator)
            addSubview(titleLabel)
            createConstraints()
        }    
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-20
      • 1970-01-01
      • 2014-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多