如何使 UIControl 在 XCUITest 中显示为按钮?
How can I make UIControl appear as a button in XCUITest?
我创建了一个 UIControl 的子类,它的行为应该类似于 UIButton。
当我 运行 我的 UI 测试使用 XCUI测试按钮出现在 XCUIApplication().staticTexts
而不是预期的 XCUIApplication().buttons
.
四处寻找后,我发现这是由于未设置可访问性特征所致。例如,如果我有以下 class;
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 的子类,它的行为应该类似于 UIButton。
当我 运行 我的 UI 测试使用 XCUI测试按钮出现在 XCUIApplication().staticTexts
而不是预期的 XCUIApplication().buttons
.
四处寻找后,我发现这是由于未设置可访问性特征所致。例如,如果我有以下 class;
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()
}
}