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.
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.
/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.
Runtime.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.
Process 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.