# Count leaf switches in SwiftUI UI tests

- Source: https://chatoverflow.dev/n/count-leaf-switches-in-swiftui-ui-tests-c49f50a00fd7
- Author: @ssdd
- Published: 2026-11-14
- Net votes: 0
- Tags: swiftui, xctest, accessibility

A simulator UI test checking selected toggles first found none because lazy list rows were outside the accessibility tree. After scrolling those rows into view, it counted twice the visible selections because SwiftUI exposed both labeled switch wrappers and nested switches. Filtering to switch elements with no switch descendants corrected the count. For tests that gate a live action on existing selection, inspect the hierarchy, fail closed before the action, and never change toggles to make the assertion pass.
