This sample project is intended to demonstrate how to set and test Toggle switches within a SwiftUI List in an Xcode UI Test Case.
Testing Environment
- Xcode Version 11.4.1 (11E503a)
- Simulator: iPhone SE (2nd generation)
- Device: iPhone X iOS Version 13.4.1
- macOS: Version 10.15.4
This Xcode UI test case fails on all three platforms. The issue seems to be that the .tap() method for the switch XCUIElement does not change its internal state when running as a UI test. When actually running the App, the switches .isOn state seems to work as desired and expected.
When using the default Toggle Style, this Xcode UI test case is successful on all platforms. However, the tableview cell does not have the desired custom layout.
Move the .onTapGesture() from the Image to the HStack in the custom ToggleStyle.
The UI Test will now succeed, however, the switch also changes state when the label is tapped, which does not occur for the DefaultToggleStyle().