Start Appium Server Programmatically Using Java
Executing Appium tests for Android and iOS requires Appium server running in background. While you can always start the server manually before running your tests, this may not be a good approach if you are targeting full automation (Eg. while running your tests in CI). Therefore it’s recommended to start Appium server programmatically. This post shows how we can achieve this using Java for Mac and Windows platforms.
Some background:
Appium server is basically a JavaScript server built using NodeJS. So to start it, we’ll need to supply Appium server JavaScript file to NodeJS executable. We also need to pass additional arguments if required.
Armed with this much information, let’s get into the meat of the matter.
Start Appium server programmatically on Mac:
- Write below shell script and save it in your system. Here we are supplying NodeJS executable (please note here that NodeJS executable is supplied by Appium itself) followed by Appium.js file and additional arguments such as port and address. You might need to modify these as per your requirements.
1/Applications/Appium.app/Contents/Resources/node/bin/node /Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js --address 127.0.0.1 --bootstrap-port 4724 --no-reset --local-timezone - Write below Java code (preferably in your setup method) to execute the shell script we have written. You may want to add some delay for Appium server to start after this code.
1Runtime.getRuntime().exec("PATH TO YOUR SHELL SCRIPT"); - The above code will start Appium server on address 127.0.0.1 port 4724 as specified in shell script.
Start Appium server programmatically on Windows:
- The procedure to start Appium server is more or less same as Mac. You’ll have to supply Node executable, Appium server file and additional arguments. But to execute this command, we will need to use Process class of Java.
- Write below code in your setup method. You’ll need to change Node and Appium executable as per your system.
1Process p = Runtime.getRuntime().exec("\"C:/Appium/node.exe\" \"C:/Appium/node_modules/appium/bin/Appium.js\" --no-reset --local-timezone"); - This will start Appium server on default address and port.
The obvious advantage of starting Appium programmatically is reduced manual intervention. This way testers can run their automated tests during off hours, resulting in higher efficiency. Do you know any other way we can start Appium server programmatically? Do share with us in comments.
Everything is fine, am happy about your blog. Thanks admin for sharing the unique content, you have done a great job I appreciate your effort and I hope you will get more positive comments from the web users.
You can achieve the same,using below method as well.
appiumServerStart(){
command = new CommandLine(“cmd”);
command.addArgument(“/c”);
command.addArgument(path to node.exe”);
command.addArgument(path to appium.js);
command.addArgument(“–no-reset”);
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
DefaultExecutor executor = new DefaultExecutor();
executor.setExitValue(1);
executor.execute(command, resultHandler);
Thread.sleep(300000);
}
appiumServerStop() {
command = new CommandLine(“cmd”);
command.addArgument(“/c”);
command.addArgument(“taskkill”);
command.addArgument(“/F”);
command.addArgument(“/IM”);
command.addArgument(“node.exe”);
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
DefaultExecutor executor = new DefaultExecutor();
executor.setExitValue(1);
executor.execute(command, resultHandler);
}
Hi,
I am getting this error. Do you have an idea about it ?
[31merror[39m: Could not determine your device from Appium arguments or desired capabilities. Please make sure to specify the ‘deviceName’ and ‘platformName’ capabilities
I have already passed these values in my capablities .
Hi Tried many times to start appium server programmatically in ubuntu I failed every time
Could you explain the same with more details
It will be good if you explain with an example in UBUNTU os
Hi Please,
Let me know how to do it in python too. Thanks 🙂