How can my XCode UI test detect that the screen has changed? How can my XCode UI test detect that the screen has changed? xcode xcode

How can my XCode UI test detect that the screen has changed?


You can test if elements are accessible:

Left click inside a UI test and start recording, the simulator will start and you can click on the element inside the simulator, if the element is accessible Xcode will write test code for you inside the testExample method.

enter image description here

Or run your app in the Simulator and open the Accessibility Inspector, and hover over the element as details will appear in the Inspector.

enter image description here

Apart from that, I think in your case you could verify this by adding a navigation item title for each screen i.e. self.navigationItem.title = @"myScreen";

And then verify the screen with an assertion i.e.

// let app = XCUIApplication() // SwiftUIApplication *app = [UIApplication alloc] init]; // Objective-CXCTAssertEqual(app.navigationBars.element.identifier, "myScreen") // Don't forget to import class XCTest


Assuming you have given your view controllers proper identifiers (so you can distinguish among them), you can add this to the view controllers involved:

#ifdef UITESTING- (void) viewDidAppear: animated{  [super viewDidAppear: animated];  // do your checking here}#endif

In your project's Test target, under the build settings there is a section Proprocessor. There you can set Preprocessor macros. Just add 'UITESTING=1' to the Debug and/or Release setting. The effect is that in your test build UITESTING will be defined, so all code under #ifdef UITESTING will be included by the preprocessor.


I would use snapshot testing using Facebook's Snapshot test library.

View-based testing means verifying that what the user sees is what you want the user to see. Doing this means being able to ensure that different versions of your views, or different states of your views, continue to look the same. View-based testing can be used to provide a high-level test covering a lot of use cases surrounding an object.

You can find a great article with more detail here.