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