Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KIF: is there a way to get all the accessibility labels in a current screen?

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.

like image 670
lmiguelvargasf Avatar asked Jun 05 '15 17:06

lmiguelvargasf


People also ask

What is Accessibilityvalue?

A string that represents the current value of the accessibility element.

What is accessibility label?

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.

What is accessibility label in IOS?

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.


1 Answers

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)")
like image 154
Bill Chan Avatar answered Sep 20 '22 13:09

Bill Chan