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.

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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s