如何将accessibilityIdentifier 设置为UIAlertActions?

Zon*_*ame 8 accessibility swift uitest

首先是的,我见过这个问题,但目前没有答案。

根本问题是我目前XCUITesting是本地化的应用程序,因此UIAlertActions 已本地化,因此我找不到按钮。

我可以做一个 hack,其中我将包含Localizable.strings我的 UITesting 包中的所有内容,然后在尝试获取这样的按钮时获取本地化版本。

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

另一种方法是可能做这个如此回答所说的黑客,但它太hacky并且有样板。

有没有办法accessibilityIdentifier在 UIAlertAction 上设置?

laz*_*bov 0

我不确定 2018 年的情况如何,但现在可以将可访问性标识符分配给UIAlertActions (因为它符合UIAccessibilityIdentification):

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

我在 UI 测试中使用时没有任何问题:

app.buttons["ActionIdentifier"]
Run Code Online (Sandbox Code Playgroud)