Getting out of the app you're testing really isn't that easy.
I'm using Appium (Selenium for Apps) to test an iOS app.
What I wanted to do is to press the home button, unfortunately Appium doesn't have such a functionality (as well as a good bunch of other testing tools).
So what I'm going for was to simulate the keystroke CMD+SHIFT+H, which is the simulator's equivalent to the home button.
That's also not possible with the most testing tools, as they interact 'inside' of the simulator, via the UIAutomation.
Finally I worked out the following solution (Java):
Runtime runtime = Runtime.getRuntime();
String[] args = { "osascript", "-e", "tell application \"System Events\" \n tell application \"Simulator\" to activate \n tell application \"System Events\" to keystroke \"h\" using {command down, shift down} \n end tell" };
runtime.exec(args);
At all it's really simple: executing an Applescript, which is then sending the keystroke to the simulator.
To provide some readability, here's the plain Applescript again:
tell application "System Events"
tell application "Simulator" to activate
tell application "System Events" to keystroke "h" using {command down, shift down}
end tell
Note: Make sure to esacpe the " (with \") in your code. Also make sure to insert \n after each line, because Applescript line-based.
Coming up with this solution took me a good amount of time.
I haven't found any other working solution to get out of the app, without killing the whole test AND being able to test inside iOS then.
EDIT: You are able to relaunch the app then, that's the trick!