UI tests with XCUITest: accessibilityIdentifier, Page Object and stability
The gist
XCUITest launches the app in a separate process and drives it from the outside, like a user, through the accessibility tree. The test runner cannot see the app's code: you cannot call a ViewModel method or pass a stub through init, and @testable import does not help here. All communication is arguments and environment variables at launch plus elements on screen. Hence three rules of stability: replace the backend with launch flags, find elements by accessibilityIdentifier, wait for events instead of sleeping.
The rest of this answer, with code and interview follow-ups, is in the DevPrep app.
Read the full answer in DevPrep