Start Appium Server – MAC and Windows

   appium-ios-macos

     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);
}
}

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

make it perfect !

Required Software Packages for Appium Automation on Mac OS

1_cDK_xkF4TfIFqAUl_ABv8w

      I observed that most of them are struggling to deal with automation for iOS in the MAC system even during the initial stage of the setup Software Package. Most of them are unaware of which package to be installed and how can be installed. The following table will help you easily configure the required software packages for automate iOS in MAC OS,

Sl. No. Software Packages What is the usage? How to install?
1 Homebrew Homebrew is a free and open-source software package management system that simplifies the installation of software on Apple’s macOS operating system 1. Open https://brew.sh/
2. Copy /usr/bin/ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)”
3. Open terminal in MAC and paste above copied command. Click enter.
2 Node Node is an open-source, cross-platform JavaScript run-time environment that executes JavaScript code outside of a browser. Here npm will get from Node. 1. Open terminal.
2. Type brew install node. Click enter
3 Appium server Appium server listen the client commands and response with actionson device side. 1. Open terminal.
2. Type npm install -g appium. Click enter
4 Appium Doctor Attempts to diagnose and fix common Node, iOS and Android configuration issues before starting Appium 1. Open terminal.
2. Type npm install -g appium-doctor. Click enter
5 Carthage It was the first dependency manager to work with Swift; in fact, Carthage itself is written in Swift! It exclusively uses dynamic frameworks instead of static libraries – this is the only way to distribute Swift binaries that are supported by iOS 8 and up. 1. Open terminal.
2. Type brew install carthage. Click enter
6 libimobiledevice In order to run our tests on real devices, we need to install libimobiledevice dependency 1. Open terminal.
2. Type brew install libimobiledevice. Click enter
7 mobiledevice mobiledevice is a command line utility for interacting with Apple’s Private Mobile Device Framework. It can be used for automating some tasks like installing and uninstalling apps on your iPhone/iPad without having to manually do it via Xcode or iTunes. You don’t need a jailbroken device! 1. Open terminal.
2. Type brew install mobiledevice. Click enter
8 Authorize-ios authorize-ios is a utility which pre-authorizes to run UIAutomation scripts on iOS devices. We need this tool for running our tests on real devices. 1. Open terminal.
2. Type npm install -g authorize-ios. Click enter
9 ios-deploy To deploy our apps to our devices by using ios-deploy. We can install and debug apps from the command line without using Xcode. 1. Open terminal.
2. Type brew install ios-deploy. Click enter
10 ideviceinstaller ideviceinstaller helps us to install, uninstall, upgrade, archive, restore and enumerate installed or archived apps. 1. Open terminal.
2. Type sudo xcode-select -r. Click enter
3. Type brew install ideviceinstaller. Click enter
11 ios_webkit_debug_proxy In order to access web views on real iOS devices, we need to install this tool. 1. Open terminal.
2. Type brew install ios-webkit-debug-proxy. Click enter
12 Maven It is used for building and managing Java-based projects by using dependencies. 1. Open terminal.
2. Type brew install maven. Click enter

     Please use all above mentioned packages prior to start scripting for iOS applications in MAC system.

make it perfect !