Start Appium Server 1.x – MAC and Windows

     In this article introducing the AppiumServiceBuilder functionality built into the Appium Java client. This will help 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 I am 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. Here I defined following private functions to work with this utility,

  • getPort – This function helps to generate random dynamic port number which 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 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()) {
System.out.println(service);
// Load the Desired Capabilities
} else {
System.out.println("Server startup failed");
System.exit(0);
}
}

     Make sure that the Selenium dependency version should be below 4 (recommended version is 3.141.59), java-client version should be less than 8.x (recommended version is 7.6.0) and appium server version should be less than 2.x (recommended version is 1.22.0)

     Please try to use the above logic to start the Appium Driver services prior load the desired capabilities.

make it perfect !

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: