Wizard of Kneup
Wizard of Kneup

Reputation: 2183

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

I have some buttons in my user interface that only display an image and no title. How can I access them during a UI Test? window.buttons["delete"].click() doesn't find the button due to lack of title. And I cannot set the title because the image has some transparency.

Upvotes: 9

Views: 8523

Answers (2)

Oletha
Oletha

Reputation: 7659

You should set an accessibilityIdentifier. This is a non-user-facing property which was designed to allow you to identify elements in your UI tests. It was introduced to stop people from abusing accessibilityLabel, which people would previously use for identifying things in their tests, but which had an effect on the experience of Voiceover users, who hear the contents of accessibilityLabel when they select an element.

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

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

Upvotes: 13

Wizard of Kneup
Wizard of Kneup

Reputation: 2183

You can find buttons through the accessibilityLabel. So, first set the label:

deleteButton.setAccessibilityLabel("delete")

And then you can access it as normal with:

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

Upvotes: 3

Related Questions