How to launch and stop appium server in Mac OS (Yosemite) from Script level (Java)

Hi ,
I am using Mac OS and Java for appium automation
I wanted to start and stop appium server from the script end instead of through the terminal.
Currently before executing the appium script, each every time i need to start the appium in terminal by texting “appium” and after execution i need to stop the serve “Kill -9 [process]”.
Instead of doing this way , how to start and stop appium from the test script itself?
can any one guide me or anyone tried and succeded in it

could you pls clarify your question , what exactly do you try to achieve ?

Currently before executing the appium script, each every time i need to start the appium in terminal by texting “appium” and after execution i need to stop the serve “Kill -9 [process]”.
Instead of doing this way , how to start and stop appium from the test script itself? Thanks.

1 Like

@Bharathjpss in order to “kill the session” you may use driver.quit after your test finishes.
If you want to terminate appium server process , you still need to “kill” , what I’m doing is to execute “killall node” command within my test
just use @AfterStory annotation (or anything similar depending the test framework your using"

Hi We used something as shown below to start the appium form the test,
StringBuffer output = new StringBuffer();

    Process appiumProcess;
    try {
        appiumProcess = Runtime.getRuntime().exec("sh /Users/Bharathan/Projects/Appium1/start_server.sh");
        appiumProcess.waitFor();
        BufferedReader reader =
                new BufferedReader(new InputStreamReader(appiumProcess.getInputStream()));

        String line = "";
        while ((line = reader.readLine())!= null) {
            output.append(line + "\n");
            System.out.println(output);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

we had used “appium” inside the “start_server.sh”
we tried to launch appium server from the test but its not working.
do you have any idea how to do this,
Similarly can you elaborate how you are killing the appium process from the test.

Why do you try to launch the server from the test ?
I suggest to do it independently before the test being executed

Do you use Appium with CI (Jenkins / TeamCity / etc …) ?

This will help you achieve what you want:
https://github.com/Genium-Framework/Appium-Support/wiki

1 Like

Hi all ,

After lots of search, below is the very straight forward code where everything automated like ios webkit debug proxy, appium launch and closes the server after the execution. hit like and share if it works.

ping me to my linkedin page for any issues,
https://www.linkedin.com/in/sethu2

//customize the below in start server method
//Webkit Proxy command
CommandLine iOSProxyCommand = new CommandLine(“ios_webkit_debug_proxy”);
iOSProxyCommand.addArgument("-c");
iOSProxyCommand.addArgument(udid+":27753");//provide your udid of the device
iOSProxyCommand.addArgument("-F");//to disable console output in eclipse

DefaultExecuteResultHandler iOSProxyresultHandler = new DefaultExecuteResultHandler();
DefaultExecutor iOSProxyexecutor = new DefaultExecutor();
iOSProxyexecutor.setExitValue(1);
try {
iOSProxyexecutor.execute(iOSProxyCommand, iOSProxyresultHandler);
iOSProxyCommand.toString()));
Thread.sleep(5000);
System.out.println(“iOS Proxy started.”);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}

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("–full-reset", false);
command.addArgument("–log-level", false);//to disable console output in eclipse
command.addArgument(“error”);
command.addArgument("–log", false);
Timestamp currentTimestamp = new java.sql.Timestamp(Calendar.getInstance().getTime().getTime());
command.addArgument("/Users/jkaspa/appium"+currentTimestamp+".log");
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
DefaultExecutor executor = new DefaultExecutor();
executor.setExitValue(1);
try {
executor.execute(command, resultHandler);
Thread.sleep(5000);
System.out.println(“Appium server started.”);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

//customize the below in stop appium server-

//kill appium node after end of your execution
String[] command = { “/usr/bin/killall”, “-9”, “node” };
try {
Runtime.getRuntime().exec(command);
System.out.println(“Appium server stopped.”);
} catch (IOException e) {
e.printStackTrace();
}
//Kill webkit proxy for iOS
String[] commandProxy = { “/usr/bin/killall”, “-9”, “ios_webkit_debug_proxy” };
try {
Runtime.getRuntime().exec(commandProxy);
System.out.println(“iOS Webkit proxy stopped”);
} catch (IOException e) {
e.printStackTrace();
}

1 Like

That actually works, you saved my day.

1 Like