简体   繁体   English

如何将accessibilityIdentifier设置为UIAlertController?

[英]How can I set accessibilityIdentifier to UIAlertController?

This is how I simply create UIAlertController and present it on the screen: 这就是我简单地创建UIAlertController并将其呈现在屏幕上的方式:

private class func showAlertWithTitle(title: String, message: String) {

    let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert)
    //alert.accessibilityLabel = "my string here"      //doesnt work
    let action = UIAlertAction(title: "OK", style: .Default) { action in
        alert.dismissViewControllerAnimated(true, completion: nil)
    }

    alert.addAction(action)
    UIStoryboard.topViewController()?.presentViewController(alert, animated: true, completion: nil)
}

and this is how I access it under UITests : 这就是我在UITests下访问它的方式

emailAlert = app.alerts["First Name"] //for title "First Name"

but I would like to set there custom identifier and access this by firstName like this: 但是我想设置自定义标识符并通过firstName访问它,如下所示:

emailAlert = app.alerts["firstName"]

Is it possible? 可能吗?

This is an old thread but someone might use this. 这是一个旧线程,但有人可能会使用它。

I was able to set the accessibility identifier like this: 我能够像这样设置辅助功能标识符:

let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.view.accessibilityIdentifier = "custom_alert"
alert.view.accessibilityValue = "\(title)-\(message)"

alert.addAction(
    UIAlertAction(
        title: "ALERT_BUTTON_OK".localized,
        style: .default,
        handler: handler
    )
)

present(alert, animated: true)

That way I can access the alert by accessibility identifier and check its contents in accessibility value. 这样我就可以通过辅助功能标识符访问警报,并在辅助功能值中检查其内容。

It is not perfect of course, but it works - at least for my testing using Appium. 它当然不是完美的,但是它起作用 - 至少对我使用Appium进行测试。

The only way I figured out to do this was to use Apple's private APIs. 我想出这样做的唯一方法是使用Apple的私有API。 You call valueForKey on the UIAlertAction object with this super secret key: "__representer" to get whats called a _UIAlertControllerActionView . 您可以使用此超级密钥在UIAlertAction对象上调用valueForKey"__representer"以获取名为_UIAlertControllerActionView

    let alertView = UIAlertController(title: "This is Alert!", message: "This is a message!", preferredStyle: .Alert)
    let okAction = UIAlertAction(title: "OK", style: .Default, handler: nil)

    alertView.addAction(okAction)

    self.presentViewController(alertView, animated: true, completion: {
        let alertButton = action.valueForKey("__representer")
        let view = alertButton as? UIView
        view?.accessibilityIdentifier = "okAction_AID"
    })

This has to be done in the completion handler because that that _UIAlertControllerActionView won't exist until the view is presented. 这必须在完成处理程序中完成,因为在显示视图之前_UIAlertControllerActionView将不存在。 On a side note in my project I used these following extensions to make things easier / more readable: 在我的项目的旁注中,我使用以下扩展来使事情更容易/更易读:

extension UIAlertController {
    func applyAccessibilityIdentifiers()
    {
        for action in actions
        {
            let label = action.valueForKey("__representer")
            let view = label as? UIView
            view?.accessibilityIdentifier = action.getAcAccessibilityIdentifier()
        }

    }

}

extension UIAlertAction
{
    private struct AssociatedKeys {
        static var AccessabilityIdentifier = "nsh_AccesabilityIdentifier"
    }

    func setAccessibilityIdentifier(accessabilityIdentifier: String)
    {
        objc_setAssociatedObject(self, &AssociatedKeys.AccessabilityIdentifier, accessabilityIdentifier, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
    }

    func getAcAccessibilityIdentifier() -> String?
    {
        return objc_getAssociatedObject(self, &AssociatedKeys.AccessabilityIdentifier) as? String
    }
}

So the above code would be rewritten: 所以上面的代码将被重写:

    let alertView = UIAlertController(title: NSLocalizedString("NMN_LOGINPAGECONTROLLER_ERROR_TITLE", comment: ""), message: message as String, preferredStyle:.Alert)
    let okAction = UIAlertAction(title: NSLocalizedString("NMN_OK", comment: ""), style: .Default, handler: nil)
    okAction.setAccessibilityIdentifier(InvalidLoginAlertView_AID)


    alertView.addAction(okAction)


    self.presentViewController(alertView, animated: true, completion: {
        alertView.applyAccessibilityIdentifiers()
    })

My first attempt involved trying to navigate the view hierarchy but that became difficult since UIAlertControllerActionView was not a part of the public API. 我的第一次尝试涉及尝试导航视图层次结构但由于UIAlertControllerActionView不是公共API的一部分而变得困难。 Anyway I'd probably would try to ifdef out the valueForKey("__representer") for builds submitted for the app store or Apple might give you a spanking. 无论如何,我可能会尝试为应用程序商店提交的构建版本为ifForf out valueForKey("__representer") ,否则Apple可能会给你打屁股。

Right now I have a UIAlertAction called addCamera and I'm just doing: 现在我有一个UIAlertAction称为addCamera ,我只是做:

addCamera.accessibilityLabel = "camera-autocomplete-action-photo"

That allows me to tap it in UI Tests as follows: 这允许我在UI测试中点击它,如下所示:

app.sheets.buttons["camera-autocomplete-action-photo"].firstMatch.tap()

From Apple docs... 来自Apple文档......

https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/UIKitUICatalog/UIAlertView.html https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/UIKitUICatalog/UIAlertView.html

Making Alert Views Accessible 使警报视图可访问

Alert views are accessible by default. 默认情况下可以访问警报视图。 Accessibility for alert views pertains to the alert title, alert message, and button titles. 警报视图的可访问性与警报标题,警报消息和按钮标题有关。 If VoiceOver is activated, it speaks the word “alert” when an alert is shown, then speaks its title followed by its message if set. 如果VoiceOver被激活,当显示警报时它会说出“警告”一词,然后说出其标题,然后是设置的消息。 As the user taps a button, VoiceOver speaks its title and the word “button.” As the user taps a text field, VoiceOver speaks its value and “text field” or “secure text field.” 当用户点击按钮时,VoiceOver会说出其标题和单词“按钮”。当用户点击文本字段时,VoiceOver会说出其值和“文本字段”或“安全文本字段”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM