We have seen how to start and stop Appium server manually and now in this post we will come to know how to start and stop Appium server programmatically. Appium provide APIs so that you can start the server before running your test case or test suit and then you can stop the server once the execution is over.
Once you install appium you the files node.exe and appium.js will be there is your system. You need the path of these two files for starting apium server programmatically. Then you can copy and paste the below code. Call appiumStart() in @BeforeClass or @BeforeTest .
Required Jar files are commons-validator-1.4.1.jar and java-client-3.2.0.jar which you can download from here.
Video -
Explanation about the code -
AppiumDriverLocalService class provied the api to start and stop the server hence we have used this in our code below.usingPort() method is used to provide port number for starting the server. We ned to pass our node.exe path to method usingDriverLocation() and appium.js path to withAppiumJs() method. Then start() and stop() methods are used to start and stop the server. We need to use getUrl() method to get and pass the url while setting up the Desired Capabilities.
import java.io.File; import io.appium.java_client.service.local.AppiumDriverLocalService; import io.appium.java_client.service.local.AppiumServiceBuilder; public class AppiumServerStartStop { static String Appium_Node_Path="C:\\Program Files (x86)\\Appium\\node.exe"; static String Appium_JS_Path="C:\\Program Files (x86)\\Appium\\node_modules\\appium\\bin\\appium.js"; static AppiumDriverLocalService service; static String service_url; public static void appiumStart() throws Exception{ service = AppiumDriverLocalService.buildService(new AppiumServiceBuilder(). usingPort(2856).usingDriverExecutable(new File(Appium_Node_Path)). withAppiumJS(new File(Appium_JS_Path))); service.start(); Thread.sleep(25000); service_url = service.getUrl().toString(); } public static void appiumStop() throws Exception{ service.stop(); } }
Note use the service_url like shown below while setting up Desired Capabilities-
AppiumDriver driver= new AndroidDriver(new URL(service_url),cap);
If you find this Post useful do share with your friends and if you have some questions or suggestions do share them with me in the comments section below.Please follow QA Automated for latest updates.
















