How can I set accessibilityIdentifier to UIAlertActions?
Asked Answered
I

1

11

First of all yes I have seen this so question but there is currently no answer.

The root problem is that I am currently XCUITesting my app which is localized so the UIAlertActions are localized so I can't find the button.

I could do a hack wherein I'll include all the Localizable.strings on my UITesting bundle then get the localized version when trying to fetch the button like this.

let localizedAlertTitle = ...(some function to fetch localized name)
let localizedButtonName = ...(some function to fetch localized name)
self.app
    .alerts[localizedAlertTitle]
    .buttons[localizedButtonName]
    .tap()

Another way is to probably do the hack said by this so answer but it's too hacky and has boilerplate.

Is there a way to set the accessibilityIdentifier on a UIAlertAction?

Ito answered 24/5, 2018 at 11:13 Comment(1)
I am facing exactly the same problem. Any good solution?Manganite
W
0

I'm not sure what situation was with this in 2018, but nowadays accessibility identifier can be assigned to UIAlertActions (since it conforms to UIAccessibilityIdentification):

let action = UIAlertAction(title: "Title", style: .destructive) { _ in
  // Handle action here.
}
action.accessibilityIdentifier = "ActionIdentifier"

And I have no problems to use in in my UI tests:

app.buttons["ActionIdentifier"]
Wiles answered 1/12, 2022 at 9:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.