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

mar*_*den 0 ios xcode-ui-testing

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

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

mar*_*den 5

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

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

}
Run Code Online (Sandbox Code Playgroud)