I’ve recently started working with SwiftUI and have been trying to implement Maestro testing into my app. Maestro accesses various things by looking at their accessibilityIdentifier
property, this way you can get the relevant button to simulate a button presses on or check a specific text box has the text you want. I have been adding these properties fine so far but I have been unable to add a unique accessibilityIdentifier
to a button with a label. Here is my (slightly simplified) button:
Button {
//Button implementation
} label: {
ZStack {
HStack {
Text("Text")
.accessibilityIdentifier("Textbox")
Image(.rightArrow)
}
.accessibilityElement(children: .contain)
.accessibilityIdentifier("Container")
}
}
All this does is make the Button element have the “Textbox” identifier. I want the Label to have 2 unique labels, one for the container and one for the text and I may add an identifier for the image in future. Would this even be possible?
Button only has one identifier
Textbox identifier is used
I looked at this post as I was initially struggling to even get the inner child elements to have unique identifiers but using .accessibilityElement(children: .contain)
before the .accessibilityIdentifier("Container")
seemed to do the job. It stopped working correctly as soon as I put it inside a button label though so I hope I can get the same functionality just inside a button
1
Something like:
Button { // Action } label: {
//label
}
.acceciblityIdentifier("Example")
You could also try to have the inner child elements inherit the same identifier and use them in different contexts since I don’t think any one of them are of the same View Type
1