This is how I simply create UIAlertController
and present it on the screen:
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:
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:
emailAlert = app.alerts["firstName"]
Is it possible?
From Apple docs...
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. 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.”
Right now I have a
UIAlertAction
calledaddCamera
and I'm just doing:addCamera.accessibilityLabel = "camera-autocomplete-action-photo"
That allows me to tap it in UI Tests as follows:
app.sheets.buttons["camera-autocomplete-action-photo"].firstMatch.tap()
The only way I figured out to do this was to use Apple's private APIs. You call
valueForKey
on theUIAlertAction
object with this super secret key:"__representer"
to get whats called a_UIAlertControllerActionView
.This has to be done in the completion handler because that that
_UIAlertControllerActionView
won't exist until the view is presented. On a side note in my project I used these following extensions to make things easier / more readable:So the above code would be rewritten:
My first attempt involved trying to navigate the view hierarchy but that became difficult since
UIAlertControllerActionView
was not a part of the public API. Anyway I'd probably would try to ifdef out thevalueForKey("__representer")
for builds submitted for the app store or Apple might give you a spanking.This is an old thread but someone might use this.
I was able to set the accessibility identifier like this:
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.