Search code examples
iostestingkifearlgrey

How does framework like KIF or EarlGrey access the running application?


Usually in iOS unit tests, we create new objects, call the method we wanna test, and then validate the results. This is a stand-alone procedure. The test cases always start running with the app instance but we do not access that instance directly.

However, with the framework like KIF or EarlGrey, we are able to write functional tests by accessing UI elements with accessibility labels in the running app instance. I am wondering how it is implemented. We don't have something like context or root view controller object when tests start, how does the framework find the presenting view controller from "nowhere"?


Solution

  • Because they are based on XCTest's Unit Test paradigm. In it, the tests and the app are both in the same bundle and therefore have access to the app internals.

    Using [UIApplication sharedApplication], you can actually get the UIWindow for the app and find the entire View Hierarchy.