【发布时间】:2016-12-09 11:46:20
【问题描述】:
我创建了一个 UIControl 的子类,它的行为应该类似于 UIButton。
当我使用 XCUITest 运行 UI 测试时,按钮出现在 XCUIApplication().staticTexts 而不是预期的 XCUIApplication().buttons。
【问题讨论】:
标签: ios xcode-ui-testing
我创建了一个 UIControl 的子类,它的行为应该类似于 UIButton。
当我使用 XCUITest 运行 UI 测试时,按钮出现在 XCUIApplication().staticTexts 而不是预期的 XCUIApplication().buttons。
【问题讨论】:
标签: ios xcode-ui-testing
在四处寻找之后,我发现这归结为未设置可访问性特征。例如,如果我有以下课程;
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()
}
}
【讨论】: