TextField with accessibilityRepresentation breaks typeText for UI tests

Using accessibilityRepresentation on a TextField breaks the UI tests ability to access the field.

It's pretty simple to reproduce. Having found a workaround that doesn't involve removing the custom representation during UI testing.

struct ContentView: View {
    @State var text = ""

    var body: some View {
        VStack {
            TextField(text: $text, axis: .vertical) {
                EmptyView()
            }
            .frame(maxHeight: .infinity, alignment: .topLeading)
            .padding(8)
            .foregroundStyle(.black) // text color
            .tint(.blue) // caret color
            .overlay {
                RoundedRectangle(cornerRadius: 4)
                    .fill(.clear)
                    .strokeBorder(.gray)
            }
            .accessibilityRepresentation {
                // axis: .vertical does weird things to VO
                // So the representation is a textfield without that
                TextField(text: $text, prompt: nil) {
                    EmptyView()
                }
            }
            .accessibilityLabel("Text area")
        }
        .padding()
    }
}
func testExample() throws {
    // UI tests must launch the application that they test.
    let app = XCUIApplication()
    app.launch()

    let element = app.textFields["Text area"]
//        let element = app.textViews["Text area"] // when no customRepresentation used
    element.tap()
    element.typeText("Hello")
}

Have you tried adding an explicit accessibility identifier to your accessibilityRepresentation TextField? I'm curious if the accessibilityRepresentation has a frame in a UI testing context, or if the exists property returns true for it when it's given a specific accessibility identifier

I'm having a similar issue with using Stepper in accessibilityRepresentation.

When I use Stepper outside of the representation, I get access to increment and decrement button, as seen in the console screenshot here. When it's within .accessibilityRepresentation the automation tests only register the QuantityStepper accessibilityId and the QuantityStepper-Decrement and QuantityStepper-Increment buttons aren't available.

I think accessibilityRepresentation would give the user the best experience, but this is preventing me from using it.

It would be great to file a feedback report on this! The team would love to hear about this

https://feedbackassistant.apple.com/

TextField with accessibilityRepresentation breaks typeText for UI tests
 
 
Q