27
Mar

How to start Appium server using AppiumDriverLocalService

To use this method to start Appium server, you need to first make sure that the following pre-requisites are met –

Node.js should be installed on your machine
Appium server should have been installed via NPM

If you have not installed Appium using NPM, then please follow this link for the setup process – Download and Install Appium using NPM here http://www.automationtestinghub.com/download-and-install-appium-1-6/

Let us now see how you can start Appium server from Java using this method. There are two main classes which you will be using here –

AppiumDriverLocalService : This is the class which is used to start and stop the Appium server
AppiumServiceBuilder : This class is used to build the Appium service, i.e. you can use this class to set the server address, port, desired capabilities and other flags before starting the Appium server
Simplest Implementation:
The simplest way to use this service is where you just use the default service and then start/stop the Appium server. The code for this implementation is given below –

AppiumDriverLocalService service = AppiumDriverLocalService.buildDefaultService();
service.start();
//your test scripts logic…
service.stop();

AppiumDriverLocalService service = AppiumDriverLocalService.buildDefaultService();
service.start();
//your test scripts logic…
service.stop();
Once you execute the above code, you will see from Eclipse Console window that Appium server has been started. Please check that the since you used the buildDefaultService() method, the port and server used are 0.0.0.0:4723, as shown below.

Implementing AppiumServiceBuilder to add more arguments:
With AppiumServiceBuilder, you have the option to configure the Appium Server, where you can add a different address, port, capabilities and other server flags. The code given below illustrates how this can be done –

And you can start the Appium server only if its not already running. The logic here is to figure out if the port is in use or not. Generally, the ports that you use with Appium are not used by other applications. So if the port is already in use, it is safe to assume that Appium server is running on the port.

This way you start the Appium server only if the port is not in use.

import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServiceBuilder;
import io.appium.java_client.service.local.flags.GeneralServerFlag;

public class AppiumServerJava {

private AppiumDriverLocalService service;
private AppiumServiceBuilder builder;
private DesiredCapabilities cap;

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

public boolean checkIfServerIsRunnning(int port) {

boolean isServerRunning = false;
ServerSocket serverSocket;
try {
serverSocket = new ServerSocket(port);
serverSocket.close();
} catch (IOException e) {
//If control comes here, then it means that the port is in use
isServerRunning = true;
} finally {
serverSocket = null;
}
return isServerRunning;
}

public static void main(String[] args) {
AppiumServerJava appiumServer = new AppiumServerJava();

int port = 4723;
if(!appiumServer.checkIfServerIsRunnning(port)) {
appiumServer.startServer();
appiumServer.stopServer();
} else {
System.out.println(“Appium Server already running on Port – ” + port);
}
}
}

Note that, with this approach we have used buildService() method instead of buildDefaultService() method. When you run this code, you will see that the Appium server is now started with all the capabilities and flags that you had provided with the service builder.

Have a look at these pages to get more information on these classes and the methods that you can use – AppiumDriverLocalService (https://appium.github.io/java-client/io/appium/java_client/service/local/AppiumDriverLocalService.html) and AppiumServiceBuilder (https://appium.github.io/java-client/io/appium/java_client/service/local/AppiumServiceBuilder.html)