Search code examples
macosxcode-ui-testing

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.


Solution

  • 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"]