How to start Appium Server Using JAVA? – pCloudy – Question and Answers
Home >> Appium Tricks and Tricks >> How to start Appium Server Using JAVA? ...

How to start Appium Server Using JAVA?


The Java client library has the facility for starting and stopping an Appium Server. Please follow the sample code mentioned below.

import java.io.File;
import java.util.HashMap;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServiceBuilder;
public class MyAppiumServer {

	public static void main(String[] args) {
//		Create instance for Service Builder class
		AppiumServiceBuilder serviceBuilder = new AppiumServiceBuilder();
		serviceBuilder.usingPort(1234);

//		Share node installation details to service builder
		serviceBuilder.usingDriverExecutable(new File("/usr/local/bin/node"));
		
//		Share appium installation details to service builder
		serviceBuilder.withAppiumJS(new File("/usr/local/bin/appium"));

//		PATH variable.
		HashMap<String, String> environment = new HashMap();
		environment.put("PATH", "/usr/local/bin:" + System.getenv("PATH"));
		environment.put("ANDROID_HOME", "/home/opuser/Android/Sdk");
		serviceBuilder.withEnvironment(environment);

		AppiumDriverLocalService server = AppiumDriverLocalService.buildService(serviceBuilder);

//		To start the server
		server.start();
		
		DesiredCapabilities capabilities = new DesiredCapabilities();
		capabilities.setCapability("newCommandTimeout", 600);
		capabilities.setCapability("launchTimeout", 90000);
		capabilities.setCapability("deviceName", "GSD***3545***");
		capabilities.setCapability("platformName", "Android");
		capabilities.setCapability("appPackage", "com.pcloudy.appiumdemo");
		capabilities.setCapability("appActivity", "com.ba.mobile.LaunchActivity");
		capabilities.setCapability("rotatable", true);
		capabilities.setCapability("app", System.getProperty("user.dir") + "/pCloudy Appium Demo.apk");

		AppiumDriver driver = new AndroidDriver(server.getUrl(), capabilities);
		driver.removeApp("com.pcloudy.appiumdemo");
		driver.installApp("pCloudyAppiumDemo.apk");
		driver.resetApp();
		
		driver.quit();
		server.stop();
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *