Start and Stop Appium Server programmatically

There are multiple ways to start appium service, manually we can start by clicking on appium icon, start using command prompt and programmatically. In this tutorial, let us see how to start Appium server programmatically. We can achieve this with the help of Appium java Client 'AppiumDriverLocalService' class.

We will see different ways of doing this. We need to start appium and stop the appium service once we are done with tests as 'appiumservice.start()' and 'appiumservice.stop()'. With the help of 'buildDefaultService()' we can easily start and stop appium service.

Let us look at the simple example to start and stop appium server.

package com.appium.adnroid;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.service.local.AppiumDriverLocalService;

public class AndroidCalcAppExample {
	WebDriver driver;
    AppiumDriverLocalService appiumService;
    String appiumServiceUrl;
    
	@BeforeTest
	public void setUp() throws MalformedURLException {
		
        appiumService = AppiumDriverLocalService.buildDefaultService();
		appiumService.start();
        appiumServiceUrl = appiumService.getUrl().toString();
        System.out.println("Appium Service Address : - "+ appiumServiceUrl);
        
		DesiredCapabilities capabilities = new DesiredCapabilities();
		capabilities.setCapability("deviceName", "8d75c47d");
		capabilities.setCapability("platformName", "Android");
		capabilities.setCapability("appPackage", "com.android.calculator2");
		capabilities.setCapability("appActivity", "com.android.calculator2.Calculator");
		
		driver = new AndroidDriver<>(new URL(appiumServiceUrl), capabilities);
		driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
	}

	@Test
	public void Sum() {
		System.out.println("Calculate sum of two numbers");
		// Locate elements to enter data and click +/= buttons
		driver.findElement(By.xpath("//*[@text='1']")).click();
		driver.findElement(By.xpath("//*[@text='+']")).click();
		driver.findElement(By.xpath("//*[@text='2']")).click();
		driver.findElement(By.xpath("//*[@content-desc='equals']")).click();

		// Get the result text
		WebElement sumOfNumbersEle = driver.findElement(By.className("android.widget.EditText"));
		String sumOfNumbers = sumOfNumbersEle.getText();

		// verify if result is 3
		Assert.assertTrue(sumOfNumbers.endsWith("3"));
	}

	@AfterTest
	public void End() {
		System.out.println("Stop driver");
		driver.quit();
		System.out.println("Stop appium service");
		appiumService.stop();
	}

}

There is also an other way to start appium service as below by using 'AppiumDriverLocalService' class and 'AppiumServiceBuilder'

But we need to provide path of Appium node.exe file path and appium.js file path as below :-

String Appium_Node_Path="C:/Program Files/Appium/node.exe";
String Appium_JS_Path="C:/Program Files/Appium/node_modules/appium/bin/appium.js";
AppiumDriverLocalService appiumService;

appiumService = AppiumDriverLocalService.buildService(new AppiumServiceBuilder().
	                usingAnyFreePort().usingDriverExecutable(new File(Appium_Node_Path)).
	                withAppiumJS(new File(Appium_JS_Path)));
appiumService.start();

In the above code, we are using method 'usingAnyFreePort()' which Configures the appium server to start on any available port. If we want to execute tests on any specific port, we have to use method 'usingPort(port value)' which sets the port on which appium server has to be started.

Appium Tutorials: 

Comments

C:\Users\vargsh4\AppData\Local\Programs\appium-desktop\Appium.exe:1
(function (exports, require, module, __filename, __dirname) { MZ�
^
SyntaxError: Unexpected token ILLEGAL
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3

Add new comment