UI 使用 Xcode 进行测试:如何找到没有标题的按钮?

UI Testing with Xcode: How to find a button that has no title?

我的用户界面中有一些按钮只显示图像而没有标题。我如何在 UI 测试期间访问它们? window.buttons["delete"].click() 由于缺少标题而找不到按钮。而且我无法设置标题,因为图像有一些透明度。

您可以通过 accessibilityLabel 找到按钮。所以,首先设置标签:

deleteButton.setAccessibilityLabel("delete")

然后您可以正常访问它:

 untitledWindow.buttons["delete"].click()

你应该设置一个accessibilityIdentifier。这是一个非面向用户的 属性,旨在让您识别 UI 测试中的元素。它的引入是为了阻止人们滥用 accessibilityLabel,人们以前会在他们的测试中使用它来识别事物,但这对 Voiceover 用户的体验有影响,他们在他们听到 accessibilityLabel 的内容时select 一个元素。

// app code
let myButton: UIButton!
myButton.accessibilityIdentifier = "deleteButton"

// test code
let app = XCUIApplication()
let deleteButton = app.buttons["deleteButton"]