Start Appium Server 2.x using different languages

     Appium Server is the core component of the Appium architecture. It is written in Node.js and runs on the local machine or in the cloud. Appium’s API is based on the W3C WebDriver Protocol, and it has supported this protocol for years. Before the W3C WebDriver Protocol was designed as a web standard, several other protocols were used for both Selenium and Appium. These protocols were the JSONWP (JSON Wire Protocol) and (M)JSONWP (Mobile JSON Wire Protocol). The W3C Protocol differs from the (M)JSONWP protocols in a few small ways. Up until Appium 2.0, Appium supported both protocols, so that older Selenium/Appium clients could still communicate with newer Appium servers. Moving forward, support for older protocols will be removed. Appium will re-design the client libraries for Appium 2.x to work with new protocols.

     The Appium server receives requests from Appium client libraries via the W3C WebDriver Protocol or JSON Wire Protocol and invokes the driver (Android driver/iOS/Windows driver) to connect to the corresponding native testing automation frameworks to run client operations on the devices or desktop applications. The test results are then received and sent to the clients. The Appium server has the capacity to create multiple sessions to simultaneously run tests on multiple devices.

     In this article, I would like to share the concept of starting the Appium server programmatically (with help of AppiumDriverLocalService) and how it will work in different languages like Python, Java, C# and Kotlin. The prerequisites are the Appium Server’s latest version should be installed on your system and also the latest java-client library added to your project environment.

Start Appium Server in Python

     Following are the code snippet to start Appium Server session using Python programming language,

DEFAULT_PORT = 4723
DEFAULT_HOST = '127.0.0.1'

def __init__(self):
    self.service = AppiumService()
    self.service.start(args=['--address', DEFAULT_HOST, '-p', str(DEFAULT_PORT), '-pa', '/wd/hub'])
    print('Appium service started...')
    print(self.service.is_running)
Start Appium Server in Java

     Following are the code snippet to start Appium Server session using Java programming language,

AppiumDriverLocalService service = AppiumDriverLocalService
.buildService(new AppiumServiceBuilder().usingPort(getPort()).withArgument(() -> "-pa", "/wd/hub")
.withLogFile(new File(System.getProperty("user.dir") + "\Logs\Appium_logs\appiumLogs_"
+ getCurrentDateAndTime() + ".txt")));
service.start();

//To generate random avaialble port
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;
}

//Get current timestamp that suffixed to the appium log file
private static String getCurrentDateAndTime() {
 DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
 DateFormat timeFormat = new SimpleDateFormat("HH-mm-ss");
 Date date = new Date();
 String currdate = dateFormat.format(date);
 String currtime = timeFormat.format(date);
 return currdate + "_" + currtime;
}
Start Appium Server in C#

     Following are the code snippet to start Appium Server session using C# programming language,

AppiumLocalService _appiumLocalService;
_appiumLocalService = new AppiumServiceBuilder().UsingAnyFreePort().Build();
_appiumLocalService.Start();
Console.WriteLine("Appium Service Started: "+ _appiumLocalService.IsRunning);
Start Appium Server in Kotlin

     Following are the code snippet to start Appium Server session using Kotlin programming language,

var IP_ADDRESS: String = "127.0.0.1"
var bootStrapPort: String
var chromePort: String
var port: Int

if (!File(System.getProperty("user.dir") + "\\Logs\\Appium_logs").exists()) {
 (File(System.getProperty("user.dir") + "\\Logs")).mkdir()
 (File(System.getProperty("user.dir") + "\\Logs\\Appium_logs")).mkdir()
}
port = getPort()
bootStrapPort = Integer.toString(getPort())
chromePort = Integer.toString(getPort())
var service = AppiumDriverLocalService.buildService(
			AppiumServiceBuilder().withIPAddress(IP_ADDRESS).usingPort(port) .withArgument(AndroidServerFlag.BOOTSTRAP_PORT_NUMBER, bootStrapPort)
.withArgument(AndroidServerFlag.CHROME_DRIVER_PORT, chromePort)
.withArgument(() -> "-pa", "/wd/hub")
.withLogFile(File(System.getProperty("user.dir") + "\\Logs\\Appium_logs\\appiumLogs.txt")))
service.start()
if (service.isRunning()) {
 System.out.println("Appium service started..." + service)
} else {
 System.out.println("Appium service failed.....")
 System.exit(0)
}

//To generate random avaialble port
private fun getPort(): Int {
var port: Int = 0
try {
 var socket = ServerSocket(0)
 socket.setReuseAddress(true)
 port = socket.getLocalPort()
 socket.close()
} catch (e: Exception) {
   e.printStackTrace()
 }
return port
}

Start Appium Server in JavaScript or TypeScript

     Following are the code snippet to start Appium Server session using JavaScript or TypeScript programming language,

import { main as appiumServer } from 'appium';
...//
public async startAppiumSession() {
 let args = {};
 return await appiumServer(args);
}

     I hope everyone enjoyed reading the article and got some idea to start the Appium server session in different programming languages like Python, Java, C#, Kotlin and JavaScript or TypeScript. Try to utilize the code snippet in your Appium automation.

make it perfect!

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 )

Twitter picture

You are commenting using your Twitter 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: