gran_profaci gran_profaci - 2 months ago 6x
iOS Question

Is there a way in EarlGrey for me to reset the application automatically?

Say I have the following app structure:

[View Controller 1] -> tap on next button
[View Controller 2] -> Check for Title

In EarlGrey, I defined this test by doing:

[[EarlGrey selectElementWithMatcher:grey_text(@"Next Button")]
[[EarlGrey selectElementWithMatcher:grey_text(@"Screen Title")]

But now, when I want to test for
[View Controller 3]
, I notice that the app is still stuck on
[View Controller 2]
. Is there a call that I am missing that can enable me to go back to the first screen or reset the application for the next test?


As with any xctest the app is not restarted and remains in the same state that the previous test left it. You would require to reset app state explicitly in either the tearDown or setUp of the test case. You can:

  1. Write UI interactions that takes the App back to the main screen after each test case completes.

  2. Introduce a method resetApplicationForTesting in your App delegate and invoke that from the setUp method of each test case. If this logic needs to be shared with multiple test cases, consider creating a BaseTestClass which these tests cases inherit.

In the test's setUp method:

    MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
    [delegate resetApplicationForTesting];