I am using KIF for testing an iOS app, and I would like to know if there is a way to get all the accessibility labels in a current screen. I would like to get an array of strings where each element is the accessibility labels that this screen has.
A string that represents the current value of the accessibility element.
For a text input field, its accessible label would be a short description of the data the field collects. For a control that is a group of options, such as a drop-down menu or list of radio buttons or checkboxes, the group should have a label that describes the relationship of the options.
The label is a very short, localized string that identifies the accessibility element, but does not include the type of the control or view. For example, the label for a Save button is “Save,” not “Save button.” By default, standard UIKit controls and views have labels that derive from their titles.
This function can return all accessibilityLabel in the view:
func getAllAccessibilityLabel(_ viewRoot: UIView) -> [String]! {
var array = [String]()
for view in viewRoot.subviews {
if let lbl = view.accessibilityLabel {
array += [lbl]
}
array += getAllAccessibilityLabel(view)
}
return array
}
func getAllAccessibilityLabelInWindows() -> [String]! {
var labelArray = [String]()
for window in UIApplication.shared.windows {
labelArray += self.getAllAccessibilityLabel(window)
}
return labelArray
}
And call it in the KIF test:
let labelArray = getAllAccessibilityLabelInWindows()
print("labelArray = \(labelArray)")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With