# How to close ios simulator after running the test

**URL:** <https://discuss.appium.io/t/how-to-close-ios-simulator-after-running-the-test/5828>\
**Category:** Issues/Bugs\
**Created:** [September 2, 2015, 5:49am UTC](https://discuss.appium.io/t/how-to-close-ios-simulator-after-running-the-test/5828 "2015-09-02T05:49:49Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![abhijt\_zanak](https://discuss.appium.io/letter_avatar_proxy/v4/letter/a/e9bcb4/32.png) [@abhijt\_zanak](https://discuss.appium.io/u/abhijt_zanak)\
**Post date:** [September 2, 2015, 5:49am UTC](https://discuss.appium.io/t/how-to-close-ios-simulator-after-running-the-test/5828/1 "2015-09-02T05:49:49Z")

</div>

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);
```

---

<div class="post-metadata">

**Author:** ![Aleksei](https://discuss.appium.io/user_avatar/discuss.appium.io/aleksei/32/6267_2.png) [@Aleksei](https://discuss.appium.io/u/Aleksei)\
**Post date:** [September 2, 2015, 7:10am UTC](https://discuss.appium.io/t/how-to-close-ios-simulator-after-running-the-test/5828/2 "2015-09-02T07:10:11Z")

</div>

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)

```auto
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) {}
    }

```

---

<div class="post-metadata">

**Author:** ![abhijt\_zanak](https://discuss.appium.io/letter_avatar_proxy/v4/letter/a/e9bcb4/32.png) [@abhijt\_zanak](https://discuss.appium.io/u/abhijt_zanak)\
**Post date:** [September 2, 2015, 8:18am UTC](https://discuss.appium.io/t/how-to-close-ios-simulator-after-running-the-test/5828/3 "2015-09-02T08:18:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Selvi\_Ranganathan](https://discuss.appium.io/user_avatar/discuss.appium.io/selvi_ranganathan/32/1675_2.png) [@Selvi\_Ranganathan](https://discuss.appium.io/u/Selvi_Ranganathan)\
**Post date:** [September 2, 2015, 8:37am UTC](https://discuss.appium.io/t/how-to-close-ios-simulator-after-running-the-test/5828/4 "2015-09-02T08:37:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Aleksei](https://discuss.appium.io/user_avatar/discuss.appium.io/aleksei/32/6267_2.png) [@Aleksei](https://discuss.appium.io/u/Aleksei)\
**Post date:** [September 2, 2015, 8:38am UTC](https://discuss.appium.io/t/how-to-close-ios-simulator-after-running-the-test/5828/5 "2015-09-02T08:38:45Z")

</div>

working without problem on my side with command:

```auto
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:

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

```

---

<div class="post-metadata">

**Author:** ![abhijt\_zanak](https://discuss.appium.io/letter_avatar_proxy/v4/letter/a/e9bcb4/32.png) [@abhijt\_zanak](https://discuss.appium.io/u/abhijt_zanak)\
**Post date:** [September 2, 2015, 8:49am UTC](https://discuss.appium.io/t/how-to-close-ios-simulator-after-running-the-test/5828/6 "2015-09-02T08:49:59Z")

</div>

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.

 ![](https://discuss.appium.io/uploads/default/original/2X/1/18845a44d58b34a76f77e9fa4c4a04d21f523c82.png)

---

<div class="post-metadata">

**Author:** ![Aleksei](https://discuss.appium.io/user_avatar/discuss.appium.io/aleksei/32/6267_2.png) [@Aleksei](https://discuss.appium.io/u/Aleksei)\
**Post date:** [September 2, 2015, 8:57am UTC](https://discuss.appium.io/t/how-to-close-ios-simulator-after-running-the-test/5828/7 "2015-09-02T08:57:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![abhijt\_zanak](https://discuss.appium.io/letter_avatar_proxy/v4/letter/a/e9bcb4/32.png) [@abhijt\_zanak](https://discuss.appium.io/u/abhijt_zanak)\
**Post date:** [September 2, 2015, 9:16am UTC](https://discuss.appium.io/t/how-to-close-ios-simulator-after-running-the-test/5828/8 "2015-09-02T09:16:41Z")

</div>

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

 ![](https://discuss.appium.io/uploads/default/original/2X/9/933aaf5e8d1f8e4a4b9456bcbf92fb1966c0c093.png)

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) {}
}

```

}

---

<div class="post-metadata">

**Author:** ![Aleksei](https://discuss.appium.io/user_avatar/discuss.appium.io/aleksei/32/6267_2.png) [@Aleksei](https://discuss.appium.io/u/Aleksei)\
**Post date:** [September 2, 2015, 9:47am UTC](https://discuss.appium.io/t/how-to-close-ios-simulator-after-running-the-test/5828/9 "2015-09-02T09:47:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![abhijt\_zanak](https://discuss.appium.io/letter_avatar_proxy/v4/letter/a/e9bcb4/32.png) [@abhijt\_zanak](https://discuss.appium.io/u/abhijt_zanak)\
**Post date:** [September 2, 2015, 10:05am UTC](https://discuss.appium.io/t/how-to-close-ios-simulator-after-running-the-test/5828/10 "2015-09-02T10:05:35Z")

</div>

worked perfectly, thanks a lot.

---

<div class="post-metadata">

**Author:** ![lostinhogwarts](https://discuss.appium.io/letter_avatar_proxy/v4/letter/l/53a042/32.png) [@lostinhogwarts](https://discuss.appium.io/u/lostinhogwarts)\
**Post date:** [November 23, 2015, 9:30pm UTC](https://discuss.appium.io/t/how-to-close-ios-simulator-after-running-the-test/5828/11 "2015-11-23T21:30:39Z")

</div>

@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?

---

<div class="post-metadata">

**Author:** ![Aleksei](https://discuss.appium.io/user_avatar/discuss.appium.io/aleksei/32/6267_2.png) [@Aleksei](https://discuss.appium.io/u/Aleksei)\
**Post date:** [November 24, 2015, 8:10pm UTC](https://discuss.appium.io/t/how-to-close-ios-simulator-after-running-the-test/5828/12 "2015-11-24T20:10:22Z")

</div>

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.
