5

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.

4

1 回答 1

6

这个函数可以返回视图中的所有accessibilityLabel:

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
}

并在 KIF 测试中调用它:

let labelArray = getAllAccessibilityLabelInWindows()
print("labelArray = \(labelArray)")
于 2017-03-01T12:10:13.573 回答