如何使 UIControl 在 XCUITest 中显示为按钮?

Posted

技术标签:

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

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

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

【问题讨论】:

【参考方案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()
        


【讨论】:

以上是关于如何使 UIControl 在 XCUITest 中显示为按钮?的主要内容,如果未能解决你的问题,请参考以下文章