Instantly share code, notes, and snippets.

Embed
What would you like to do?
Start & Stop Appium Server Programmatically using Java (MAC OSX)
package utilities;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecuteResultHandler;
import org.apache.commons.exec.DefaultExecutor;
import java.io.IOException;
/**
*
* @author RightQA
*
*/
public class AppiumServer {
public void startServer() {
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);
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();
}
}
public void stopServer() {
String[] command = { "/usr/bin/killall", "-KILL", "node" };
try {
Runtime.getRuntime().exec(command);
System.out.println("Appium server stopped.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
@benz-philip

This comment has been minimized.

Show comment
Hide comment
@benz-philip

benz-philip May 25, 2017

The start and stop methods are good to go with if we have only one instance of appium server. What if we have multiple appium server launched and we want to stop a particular node. using given code to stop server stops all the servers launched.

welcome your suggestions...

benz-philip commented May 25, 2017

The start and stop methods are good to go with if we have only one instance of appium server. What if we have multiple appium server launched and we want to stop a particular node. using given code to stop server stops all the servers launched.

welcome your suggestions...

@Elangopalakrishnan

This comment has been minimized.

Show comment
Hide comment
@Elangopalakrishnan

Elangopalakrishnan Jun 6, 2017

I'm also having same question, what we can do if we have 2 appium server instance. @benz-philip

Elangopalakrishnan commented Jun 6, 2017

I'm also having same question, what we can do if we have 2 appium server instance. @benz-philip

@Ankit-Pare

This comment has been minimized.

Show comment
Hide comment
@Ankit-Pare

Ankit-Pare Jan 23, 2018

When tried to run the above code :
@Test(enabled = true)
public void startServer() {

	CommandLine command = new CommandLine("/Applications/Appium.app/Contents/Resources/node/bin/node");
	command.addArgument("/Applications/Appium.app/Contents/Resources/node_modules/appium/lib/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);
	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();
	}
}

@Test(enabled = false)
public void stopServer() {
	String[] command = { "/usr/bin/killall", "-KILL", "node" };
	try {
		Runtime.getRuntime().exec(command);
		System.out.println("Appium server stopped.");
	} catch (IOException e) {
		e.printStackTrace();
	}
}

}`

I got the following error :

/Applications/Appium.app/Contents/Resources/node_modules/appium/lib/appium.js:1
(function (exports, require, module, __filename, __dirname) { import _ from 'lodash';
^^^^^^

SyntaxError: Unexpected token import
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:387:25)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:146:18)
at node.js:404:3

After running the code :
Got the following message :
Default test
Tests run: 1, Failures: 0, Skips: 0
So the test ran successfully , but after trying to find if any server is working on port 4723 , using
netstat -vanp tcp | grep 4723 , i got no process running on the port.

Can anyone help me with this? TIA :)

Ankit-Pare commented Jan 23, 2018

When tried to run the above code :
@Test(enabled = true)
public void startServer() {

	CommandLine command = new CommandLine("/Applications/Appium.app/Contents/Resources/node/bin/node");
	command.addArgument("/Applications/Appium.app/Contents/Resources/node_modules/appium/lib/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);
	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();
	}
}

@Test(enabled = false)
public void stopServer() {
	String[] command = { "/usr/bin/killall", "-KILL", "node" };
	try {
		Runtime.getRuntime().exec(command);
		System.out.println("Appium server stopped.");
	} catch (IOException e) {
		e.printStackTrace();
	}
}

}`

I got the following error :

/Applications/Appium.app/Contents/Resources/node_modules/appium/lib/appium.js:1
(function (exports, require, module, __filename, __dirname) { import _ from 'lodash';
^^^^^^

SyntaxError: Unexpected token import
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:387:25)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:146:18)
at node.js:404:3

After running the code :
Got the following message :
Default test
Tests run: 1, Failures: 0, Skips: 0
So the test ran successfully , but after trying to find if any server is working on port 4723 , using
netstat -vanp tcp | grep 4723 , i got no process running on the port.

Can anyone help me with this? TIA :)

@tanumuki

This comment has been minimized.

Show comment
Hide comment
@tanumuki

tanumuki Jul 9, 2018

Getting connection refused :(
org.openqa.selenium.WebDriverException: Connection refused (Connection refused)
Build info: version: '3.12.0', revision: '7c6e0b3', time: '2018-05-08T14:04:26.12Z'
System info: host: 'Tanus-MacBook-Pro.local', ip: 'fe80:0:0:0:ce1:c81b:2075:a1f6%en0', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.13.5', java.version: '1.8.0_151'
Driver info: driver.version: IOSDriver

tanumuki commented Jul 9, 2018

Getting connection refused :(
org.openqa.selenium.WebDriverException: Connection refused (Connection refused)
Build info: version: '3.12.0', revision: '7c6e0b3', time: '2018-05-08T14:04:26.12Z'
System info: host: 'Tanus-MacBook-Pro.local', ip: 'fe80:0:0:0:ce1:c81b:2075:a1f6%en0', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.13.5', java.version: '1.8.0_151'
Driver info: driver.version: IOSDriver

@narendra-chandratre

This comment has been minimized.

Show comment
Hide comment
@narendra-chandratre

narendra-chandratre Aug 2, 2018

Connection get refused when your specified port is already occupied!
If you try to stop thread running using that port will resolve your problem or if you could change your port itself.

#AllTheBest

narendra-chandratre commented Aug 2, 2018

Connection get refused when your specified port is already occupied!
If you try to stop thread running using that port will resolve your problem or if you could change your port itself.

#AllTheBest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment