0

Is it possible to delete the app cache from the simulator each time a new UI test is executed? This is a specific use case in our environment for a number of reasons I can't discuss here.

Note that it is not possible for us to use Process() object in UI tests to execute a shell script that could clean the cache.

We need something that clears entities in core data and any sort of cache the app can keep during execution.

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116

1 Answers1

0

The way I usually deal with this issue is by defining a custom command-line argument that is handled by the AppDelegate and deletes everything before starting the rest of the app.

You just need to define an XCTestPlan configuration that passes a command-line argument such as:

XCTestPlan configuration

Then in func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool, you just need to check if that command line argument exists and do your thing:

if CommandLine.arguments.contains("EnableUITests") {
       UIView.setAnimationsEnabled(false)
       // Clear Cache
}

In the same function, I also make sure to disable animations so that UITests always give consistent results.

MrAsterisco
  • 797
  • 8
  • 21
  • // Clear Cache It is the part where I am stuck now since the dev team used different technics for storing cache. I wish there is like one-liner that just wipes out the app cache as if it is deleted. – Jonas M Jun 17 '20 at 08:38
  • Well, that depends on where things are stored. UserDefaults, for example, have a way to reset everything to default values (see https://stackoverflow.com/questions/6797096/delete-all-keys-from-a-nsuserdefaults-dictionary-ios). – MrAsterisco Jun 18 '20 at 09:04