How do I query the "+" button in XCTest?

I have a + image in the UI from the following code

    struct QueryPlusButton: View {
        var body: some View {
            Button {
            }
            label: {
                Image(systemName: "plus")
            }
        }
    }

However, I'm not able to query the + image in XCTest using any of the following queries

let view = app.images["plus"]
//let view = app.staticTexts["plus"]
//let view = app.buttons["plus"]
XCTAssertTrue(view.exists, "Plus button does not exist")

The test fails. Is there a methodical way to determine what a query is for any UI element? Thank you.

Hello! The way this should get exposed to XCTest is a Button with an identifier of "plus".

This query is working for me when I made a simple SwiftUI app with just your Button snippet:

app.buttons["plus"].tap()

So in your example this should work:

let view = app.buttons["plus"]
XCTAssertTrue(view.exists, "Plus button does not exist")

If that is still not working, it could be that due to how your QueryPlusButton is being nested inside of other SwiftUI views.

As for how to figure this out in general, there are a few ways:

  1. Use the Accessibility Inspector tool: https://developer.apple.com/documentation/accessibility/inspecting-the-accessibility-of-screens

    • when you hover over the + button it should show you the type and properties of the element in question
  2. Use print(app.debugDescription) - debugDescription will dump all of the elements, their types, and properties in the app at that time. It can be hard to read, but should give you the information you're looking for.

Once you know the type of an element (button/image/etc) and an identifier or label you can use to target it, that will help you write the query needed to target that element.

How do I query the "+" button in XCTest?
 
 
Q