How to Effectively Start Appium Server in Mac and Windows?

Share

Keep Appium Server Running on Windows and MAC   

This article introduces the AppiumServiceBuilder functionality, built into the Appium Java client. The aim is to provide help in order to start the Appium Server programmatically, in both MAC and Windows OS.

Start Your Appium Server

Using this concept, you can start many Appium Server sessions for your automation. Especially in case of parallel execution, you may need to start the Appium servers in different terminals with different ports. Here, you are creating the available ports using the ServerSocket class and binding the dynamic port with build service of AppiumDriverLocalService. Based on your Appium service availability, you can load all the desired capability and send those as a request to Appium Server. Now, you need to define the following private functions to work with this utility:

  • getPort – This function helps to generate random dynamic port number which is required to start the Appium service. The port number includes port for Appium server URL, Chrome Driver port, and Bootstrap port.
  • getNodePath – This function helps to get the path of installed node in both Windows and MAC Operating Systems.
  • getJSPath – This function helps to get the path of the main.js in both Windows and MAC Operating Systems.
  • startAppiumServer – This function helps to start the Appium Driver service in both Windows and MAC Operating Systems.

Below is the actual implementation of getPort function,

private static int getPort() throws Exception {

int port = 0;

try {

ServerSocket socket = new ServerSocket(0);

socket.setReuseAddress(true);

port = socket.getLocalPort();

socket.close();

} catch (Exception e) {

e.printStackTrace();

}

return port;

}

Below is the actual implementation of getNodePath function,

private static String getNodePath() throws IOException, InterruptedException {

String jsPaths = null;

String nodePath = null;

Process p;

BufferedReader reader;

String operatingSystem = System.getProperty(“os.name”);

if (operatingSystem.contains(“Win”)) {

String whereAppium = “where” + ” ” + “node”;

p = Runtime.getRuntime().exec(whereAppium);

reader = new BufferedReader(new InputStreamReader(p.getInputStream()));

while ((jsPaths = reader.readLine()) != null) {

nodePath = jsPaths;

break;

}

p.waitFor();

p.destroy();

if (nodePath == null) {

System.exit(0);

}

} else {

String command = “which ” + “node”;

p = Runtime.getRuntime().exec(command);

p.waitFor();

reader = new BufferedReader(new InputStreamReader(p.getInputStream()));

String line = “”;

while ((line = reader.readLine()) != null) {

nodePath = line;

break;

}

p.destroy();

if (nodePath == null) {

System.exit(0);

}

}

return nodePath;

}

Below is the actual implementation of getJSPath function,

private static String getJSPath() throws IOException, InterruptedException {

String jsPaths = null;

String actualJSPath = null;

String operatingSystem = System.getProperty(“os.name”);

if (operatingSystem.contains(“Win”)) {

String whereAppium = “where” + ” ” + “appium”;

Process p = Runtime.getRuntime().exec(whereAppium);

BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

while ((jsPaths = stdInput.readLine()) != null) {

actualJSPath = jsPaths.replace(“appium”, “node_modules\\appium\\build\\lib\\main.js”);

break;

}

p.waitFor();

p.destroy();

if (actualJSPath == null) {

System.exit(0);

}

} else {

actualJSPath = “//usr//local//lib//node_modules//appium//build//lib//main.js”;

}

return actualJSPath;

}

Below is the actual implementation to start Appium Driver Service,

public static void startAppiumServer() throws Exception {

String IP_ADDRESS = “127.0.0.1”;

String bootStrapPort;

String chromePort;

int port;

AppiumDriverLocalService service;

port = getPort();

bootStrapPort = Integer.toString(getPort());

chromePort = Integer.toString(getPort());

service = AppiumDriverLocalService.buildService(new AppiumServiceBuilder().withAppiumJS(new File(getJSPath()))

.usingDriverExecutable(new File(getNodePath())).withIPAddress(IP_ADDRESS).usingPort(port)

.withArgument(AndroidServerFlag.BOOTSTRAP_PORT_NUMBER, bootStrapPort)

.withArgument(AndroidServerFlag.CHROME_DRIVER_PORT, chromePort));

service.start();

if (service.isRunning()) {

// Load the Desired Capabilities

} else {

System.out.println(“Server startup failed”);

System.exit(0);

}

}   

Appium has been one of the most solid open-source test automation frameworks during the past few years. Automating hybrid and native mobile applications for Android and iOS is a key function handled by Appium, a node.js server. It is recommended to try and use the above logic to start the Appium Driver services prior load the desired capabilities. 

By, 
Sanoj S, Test Architect

LEAVE A COMMENT

How can we help you?