Start-Stop Appium Server Programmatically - Method 1

Start and stop appium server Programmatically means start the appium server using code when your software app test execution started and stop appium server using code when software app  test execution finished. Till now, We have learnt how to start appium server manually from start menu In THIS POST, and using command prompt as described In THIS POST. Now let me explain you how to start and stop appium server Programmatically. HERE Is another way of starting appium server programatically.

App To Use In This Test
We will use Api Demos app In this test to demonstrate you how to start appium server programmatically. Earlier we have used same software app In many appium android software test examples.

Aim To Achieve
Earlier we were starting appium server manually before running test and stopping It manually on android app software test execution completion. Now we wants to start and stop It programmatically. So we will write some logic In our test case file to start and stop appium server.

Create And Run Android App Software Test
Create new class file under Android package of your project and paste bellow given test In It.

Note 1 : In bellow given test script, If your node.exe Is located at any other path then you need change value of nodePath string. Generally It will be located at C:\Program Files\Appium.

Note 2 : Same thing applied for appium.js file. Change value of appiumJSPath string If It Is different for you.
package Android;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class AppiumStartAndStop {
 Process p;
 // Set path of your node.exe file.
 // Progra~1 represents Program Files folder.
 String nodePath = "C:/Progra~1/Appium/node.exe";
 // Set path of your appium.js file.
 String appiumJSPath = "C:/Progra~1/Appium/node_modules/appium/bin/appium.js";
 String cmd = nodePath + " " + appiumJSPath;
 AndroidDriver driver;

 // This method Is responsible for starting appium server.
 public void appiumStart() throws IOException, InterruptedException {
  // Execute command string to start appium server.
  p = Runtime.getRuntime().exec(cmd);
  // Provide wait time of 10 mins to start appium server properly.
  // If face any error(Could not start a new session...) then Increase
  // this time to 15 or 20 mins.
  if (p != null) {
   System.out.println("Appium server Is started now.");

 // This method Is responsible for stopping appium server.
 public void appiumStop() throws IOException {
  if (p != null) {
  System.out.println("Appium server Is stopped now.");

 public void setUp() throws Exception {
  // Stop appium server If It Is already running.
  // Start appium server.
  DesiredCapabilities capabilities = new DesiredCapabilities();
  capabilities.setCapability("deviceName", "ZX1B32FFXF");
  capabilities.setCapability("browserName", "Android");
  capabilities.setCapability("platformVersion", "4.4.2");
  capabilities.setCapability("platformName", "Android");
  capabilities.setCapability("appPackage", "");
  capabilities.setCapability("appActivity", "");
  driver = new AndroidDriver(new URL(""), capabilities);
  driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

 public void ScrollToView() {
  // Scroll till element which contains "Views" text If It Is not visible on screen.
  // Click on Views.

 public void End() throws IOException {
  // Stop appium server when test Is ended.

Test Script Description
In above test, appiumStart() method Is responsible to start appium server and appiumStop() method to stop appium server. We have call appiumStart() method In @BeforeTest annotated method to start appium server before test starts and call appiumStop() method In @AfterTest annotated method to stop appium server after test execution.

Don't start appium server manually and run above android app software test using testng. It will start appium server In background and run test In android device.

Next post will show you another method of starting and stopping appium server programmatically.


  1. How can we do same thing in MAC Os System

  2. Why don't you set up server address and port for Appium?