Starting appium server programmatically

Sebin Joseph
2 min readMay 18, 2021

If you are using appium for mobile test automation, are you starting the appium server manually each time via CLI or appium-desktop? Appium does allow us to start appium server via our code itself with desired host and port. Let's see how. For this article, we would be considering appium java client, but it is supported by other libraries as well.

AppiumServiceBuilder is the class that allows this. If we don’t want to specify any details and start the appium server with the default configuration, the following code will start the appium server for you.

AppiumServiceBuilder serviceBuilder = new AppiumServiceBuilder();
AppiumDriverLocalService server = AppiumDriverLocalService.buildService(serviceBuilder);
server.start();

This will start the server on 0.0.0.0:4723. But this will be executed without error only if the node and appium paths are already set as environment variables.

If we want to configure these manually, ServiceBuilder provides methods for setting host IP, port, environments, capabilities, etc. Let’s go through the most commonly used setup methods.

Setting Node and Appium Path

usingDriverExecutable can be used to set node executable path and withAppiumJS for setting appium’s main.js path.

By default, the node executable for Windows will be inside Program Files or Program Files (x86)/nodejs and for mac, if installed via homebrew, will be in /opt/homebrew/bin.

Appium can be installed via the command line using npm or as appium-desktop. For command-line installations, main.js will be available under /AppData/Roaming/npm/node_modules/appium/build/lib for Windows and /opt/homebrew/bin/appium/build/lib OR /opt/homebrew/lib/node_modules/appium/build/lib for macOS. For appium-desktop installations, main.js will be available under Program Files/Appium/resources/app/node_modules/appium/build/lib for Windows and /Applications/Appium.app/Contents/Resources/app/node_modules/appium/build/lib for mac.

serviceBuilder.usingDriverExecutable(new File("/opt/homebrew/bin/node"));serviceBuilder.withAppiumJS(new File("/Applications/Appium.app/Contents/Resources/app/node_modules/appium/build/lib/main.js"));

For appium js, the path till appium is also enough and /build/lib/main.js could be ignored.

Setting host and port

Setting host and port is not mandatory, as appium will start the server in the host IP 0.0.0.0 and the default appium port 4723. Custom host and port can be set using

serviceBuilder.withIPAddress("127.0.0.1");
serviceBuilder.usingPort(4724);

ServiceBuilder also allows choosing any random port that is available with the method

serviceBuilder.usingAnyFreePort();

Starting and Stopping server

AppiumLocalService can be instantiated after setting these required values for the service builder and the service can be started using start() which will start the appium server on your machine. None of these options are mandatory for service builder if Node and Appium are available in the environment path and you really don’t care about the host and port where the server would run. Once the test is complete, the server needs to be stopped using stop().

AppiumServiceBuilder serviceBuilder = new AppiumServiceBuilder();
serviceBuilder.withIPAddress("<appium_ip_address>");
serviceBuilder.usingPort(<port_num>)
serviceBuilder.usingDriverExecutable(new File("<node_path>"));
serviceBuilder.withAppiumJS(new File("<appium_path>"));
AppiumDriverLocalService server = AppiumDriverLocalService.buildService(serviceBuilder);
server.start();
DesiredCapabilities caps = new DesiredCapabilities();
//set the capabilities
AppiumDriver driver = new AndroidDriver<AndroidElement>(server.getUrl(), caps);//After execution
server.stop();

Happy coding!

--

--