How to close ios simulator after running the test

I have written following code in order to start the appium server and close the server after test case execution completed.however starting server works fine, closing does not work.
AppiumDriver driver;

	CommandLine command = new CommandLine("/Applications/Appium.app/Contents/Resources/node/bin/node");
	command.addArgument("/Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js", false);
	command.addArgument("--address", false);
	command.addArgument("127.0.0.1");
	command.addArgument("--port", false);
	command.addArgument("4723");
	command.addArgument("--no-reset", false);
	DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
	DefaultExecutor executor = new DefaultExecutor();
	executor.setExitValue(1);
	executor.execute(command, resultHandler);
	
	DesiredCapabilities caps = new DesiredCapabilities();
	caps.setCapability("deviceName", "iPhone 5s");
	caps.setCapability("platformVersion", "8.4");
	caps.setCapability("platformName", "iOS");
	caps.setCapability("app", "/Users/abhijit/Downloads/PreSales-Huddle.app"); 
	driver = new IOSDriver(new URL("http://127.0.0.1:4723/wd/hub"), caps);
	
	String name = driver.findElementByName("Login").getText();
	System.out.println(name);
	
	CommandLine command1 = new CommandLine("killall -9 'iOS Simulator'");
	command1.addArgument("iOS Simulator");
	DefaultExecuteResultHandler resultHandler1 = new DefaultExecuteResultHandler();
	DefaultExecutor executor1 = new DefaultExecutor();
	executor1.setExitValue(1);
	executor1.execute(command1, resultHandler1);

you may use osascript on your mac.
with first execution you should add script to allowed apps to make changes on your computer. (system preference -> security and privacy -> tab: privacy -> choose Accessibility)

example of pressing home button on simulator (just update keystroke for close)

public void pressHomeButton() {
        try {
            String[] args = {"osascript", "-e", "tell application \"System Events\" \n tell application \"iOS Simulator\" to activate \n tell application \"System Events\" to keystroke \"h\" using {command down, shift down} \n end tell"};
            Process process = Runtime.getRuntime().exec(args);
            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(process.getErrorStream()));
            String lsString;
            while ((lsString = bufferedReader.readLine()) != null) {
                System.out.println(lsString);
            }

            try{Thread.sleep(200);}catch (Exception e1){}
        } catch (Exception e) {}
    }

not working at my end , can’t we use killall ?

@abhijt_zanak, Try driver.quit() to close the close the simulator. To close the app driver.closeApp()

working without problem on my side with command:

String[] args = {"osascript", "-e", "tell application \"System Events\" \n tell application \"iOS Simulator\" to activate \n tell application \"System Events\" to keystroke \"q\" using {command down} \n end tell"};

you should prefer soft close.

otherwise example of kill as:

        String kill[] = {"killall","iOS Simulator"};
        Runtime.getRuntime().exec(kill);

Thanks.
killall worked perfectly.
However as you suggested, I should prefer soft close.as you mentioned in earlier comment
“with first execution you should add script to allowed apps to make changes on your computer. (system preference -> security and privacy -> tab: privacy -> choose Accessibility)” .
Here you mean to say that i have to add appium ? or something else , i am bit confused .
added screenshot for reference.

when you start execution of osacscript it will display dialog to allow do changes. you should allow one time and it will remember. “System Events.app” (as i remember) will appear in list near Appium.
otherwise you can add it manually from folder “/System/Library/CoreServices/” by choosing “+” on this page.

i have here in list: “System Events.app”, “SystemUIServer.app” and “Terminal.app” - not sure which one exactly using with osascript.

added apps “System Events.app”, “SystemUIServer.app” and “Terminal.app” still not working.

package amazon;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.ios.IOSElement;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecuteResultHandler;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
import org.openqa.selenium.remote.DesiredCapabilities;

public class appServer {

public static void main(String[] args) throws ExecuteException, IOException {
	AppiumDriver<IOSElement> driver;

	
	CommandLine command = new CommandLine("/Applications/Appium.app/Contents/Resources/node/bin/node");
	command.addArgument("/Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js", false);
	command.addArgument("--address", false);
	command.addArgument("127.0.0.1");
	command.addArgument("--port", false);
	command.addArgument("4723");
	command.addArgument("--no-reset", false);
	DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
	DefaultExecutor executor = new DefaultExecutor();
	executor.setExitValue(1);
	executor.execute(command, resultHandler);
	
	DesiredCapabilities caps = new DesiredCapabilities();
	caps.setCapability("deviceName", "iPhone 5s");
	caps.setCapability("platformVersion", "8.4");
	caps.setCapability("platformName", "iOS");
	caps.setCapability("app", "/Users/abhijit/Downloads/PreSales-Huddle.app"); //Replace this with app path in your system
	driver = new IOSDriver(new URL("http://127.0.0.1:4723/wd/hub"), caps);
	
	String name = driver.findElementByName("Login").getText();
	System.out.println(name);

	pressHomeButton();

}
public static void pressHomeButton() {
    try {
        String[] args = {"osascript", "-e", "tell application \"System Events\" \n tell application \"iOS Simulator\" to activate \n tell application \"System Events\" to keystroke \"h\" using {command down, shift down} \n end tell"};
        Process process = Runtime.getRuntime().exec(args);
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(process.getErrorStream()));
        String lsString;
        while ((lsString = bufferedReader.readLine()) != null) {
            System.out.println(lsString);
        }

        try{Thread.sleep(200);}catch (Exception e1){}
    } catch (Exception e) {}
}

}

In your code you pressing Home button on emulator as i first gave. Update osascript text to press quit button as i wrote in thread.

worked perfectly, thanks a lot.

@Aleksei can you explain more about this line:

String[] args = {“osascript”, “-e”, “tell application “System Events” \n tell application “iOS Simulator” to activate \n tell application “System Events” to keystroke “h” using {command down, shift down} \n end tell”};

I’ve incorporated this in my code and iOS simulator is already launched. I only need to close the app (by pressing CMD+SHIFT+H) through the java code. Would I still have to use the same String[] args you’ve mentioned?

hi. there are many ways to execute command in terminal with java. choose whatever you like. with mine example it is needed in such way.