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.
KIF: is there a way to get all the accessibility labels in a current screen?
Asked Answered
What are you trying to do? Trying to read the state of the UI is an anti-pattern. Your tests should take a known series of actions and assert they succeed. –
Deflective
I am trying to check if a table is sorted alphabetically, and I do not know how exactly achive this. Therefore, I would like to get all the accessibility labels and check if their names are letters. –
Allhallowmas
To check that a table is sorted correctly, you should just check that all of the rows have the correct values. (And if you don't know what the values should be ahead of time, consider changing your testing approach.) –
Deflective
I disagree that reading the state of the UI is an anti-pattern, particularly in tests. A good test provides debugging information when the test fails, and the complete content of a screen is a large part of that -- especially when running on CI, where you may not be able to see the screen of the target device. How one should write tests is irrelevant to the question of how to read information from the screen. –
Snakeroot
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)")
© 2022 - 2024 — McMap. All rights reserved.