How to retrieve elements within an element, using accessibility identifiers

139 Views Asked by At

I have a VStack, which loops through and prints a view, based on the number of objects within the array. Each view has a unique accessibility identifier person_x, dependant on the ID of the person.

VStack() {
    ForEach(model.people) { person in
        personView(model: person)
            .accessibilityElement(children: .contain)
            .accessibilityIdentifier("person_\(person.id)")
}

Within the personView there's additional elements which also have accessibility identifiers, but these are not "unique", they're generically named, for example:

private var carStack: some View {
    CarSwiftUIView(viewModel: viewModel.carViewModel)
        .accessibilityIdentifier("car")
}

So the hierarchy of the accessibility elements would be like this:

  1. person_1
    • car
  2. person_2
    • car
  3. person_3
    • car
  4. person_4
    • car etc..

How can I interact with a car element, within a specific person view.

The closest I got, but still without success, was:

let elements = app.descendants(matching: .any)["person_1"]

Any help would be hugely appreciated. Thank you.

0

There are 0 best solutions below