Automating Starting and Stopping Appium Server in your Appium Framework.

Khemlall Mangal
2 min readJun 4, 2022

I am currently writing a series on how to create a automation framework from scratch using TestNG, Maven, appium, selenium to test your mobile application. This framework can be adopted to testing selenium and more. Now as part of the automation framework we want to add the functionality whereby Appium server is started automatically when the test begins. There are a couple of classes available with Java client that makes it convenient to start and stop the Appium server.

One note is that Node.js has the advantage of being the language Appium is written in, so an Appium server can be started just by requireing it:

let Appium = require('appium')
let server = await Appium.main()
// server now running
await server.close()
// server stopped

In Java we have AppiumServiceBuilder which we can use to create localappium service.

In your base class define this method to start your server:

public static AppiumDriverLocalService service;public AppiumDriverLocalService startServer() {
boolean serverflag = CheckServiceState(4723);
if(!serverflag)
{

service = AppiumDriverLocalService.buildDefaultService();
service.start();

}
return service;
}

We can check the state to see if the Service is Running or not

public static boolean CheckServiceState(int port) {

boolean isServiceRunning=false;
ServerSocket serviceSocket;
try {
serviceSocket = new ServerSocket(port);
serviceSocket.close();
}catch(IOException e){
isServiceRunning=true;

}finally {
serviceSocket=null;
}
return isServiceRunning;
}

our base class define this method to start your server:

pu

There are 3 ways to achieve the scenario.

1)Using AppiumDriverLocalService

public void startServer() {
//Set Capabilities
cap = new DesiredCapabilities();
cap.setCapability("noReset", "false");
//Build the Appium service
builder = new AppiumServiceBuilder();
builder.withIPAddress("127.0.0.1");
builder.usingPort(4723);
builder.withCapabilities(cap);
builder.withArgument(GeneralServerFlag.SESSION_OVERRIDE);
builder.withArgument(GeneralServerFlag.LOG_LEVEL,"error");
//Start the server with the builder
service = AppiumDriverLocalService.buildService(builder);
service.start();
}
public void stopServer() {
service.stop();
}

2)Using Appium.js with Node.exe

public void startServer() {
CommandLine cmd = new CommandLine("C:\\Program Files (x86)\\Appium\\node.exe");
cmd.addArgument("C:\\Program Files (x86)\\Appium\\node_modules\\appium\\bin\\Appium.js");
cmd.addArgument("--address");
cmd.addArgument("127.0.0.1");
cmd.addArgument("--port");
cmd.addArgument("4723");
DefaultExecuteResultHandler handler = new DefaultExecuteResultHandler();
DefaultExecutor executor = new DefaultExecutor();
executor.setExitValue(1);
try {
executor.execute(cmd, handler);
Thread.sleep(10000);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
public void stopServer() {
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec("taskkill /F /IM node.exe");
} catch (IOException e) {
e.printStackTrace();
}
}

3)Start Appium server using Command Prompt

public void startServer() {
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec("cmd.exe /c start cmd.exe /k \"appium -a 127.0.0.1 -p 4723 --session-override -dc \"{\"\"noReset\"\": \"\"false\"\"}\"\"");
Thread.sleep(10000);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
public void stopServer() {
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec("taskkill /F /IM node.exe");
runtime.exec("taskkill /F /IM cmd.exe");
} catch (IOException e) {
e.printStackTrace();
}
}<br/>

Hope you find this helpful.

--

--

Khemlall Mangal

I am a passionate coder, QA Engineer, and someone who enjoys the outdoors.