Start and Stop Appium server programatically using LocalService in JAVA

Versions used while doing this blog post,
Appium server version : 1.21.0
Appium JAVA client version : 7.5.1

As we know, Appium is capable of automating Web, native and hybrid apps in android. In this post, lets see how we can invoke an app in android. The APK file of the app is in executors machine.

Before getting into actual coding, lets get the steps to be followed to make any appium program functional:
1. We need the latest appium java client and latest stable selenium to be referred into the project library

2. The first step of the program should be invoking the Appium server programmatically using buildDefaultService() and start() methods in AppiumDriverService class. This will start Appium in default port 4723.

appiumService = AppiumDriverLocalService.buildDefaultService();
appiumService.start();


3. The next step of the program should be setting up of desired capabilities. Here we need to provide the details of the android device with which we are gonna interact and app that is to be installed & invoked.
The mandatory parameters here are:
a. DEVICE_NAME
b. APP [As, we have the apk/app file in PC from which we execute the program]

4. Next inline should be the creation of android driver by passing appium server URL and device capabilities

5. Once done with execution of expected functionality, the session should be closed/quit

6. The final step should be terminating Appium programmatically using stop() method in AppiumDriverService class.

appiumService.stop();

Complete code:

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.net.MalformedURLException;
import java.net.URL;

public class StartAndStopAppiumProgrammaticallyUsingLocalService {
    public static AppiumDriverLocalService appiumService;

    public static void main(String[] args) throws MalformedURLException {
        startAppium();
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "3b9dd6c7");
        if (System.getProperty("os.name").toLowerCase().startsWith("mac"))
            capabilities.setCapability(MobileCapabilityType.APP, System.getProperty("user.dir")+"/src/main/resources/ApiDemos-debug.apk");
        else
            capabilities.setCapability(MobileCapabilityType.APP, System.getProperty("user.dir")+"\\src\\main\\resources\\ApiDemos-debug.apk");

        URL url = new URL("http://0.0.0.0:4723/wd/hub");
        AndroidDriver<AndroidElement> driver = new AndroidDriver<>(url, capabilities);

        driver.quit();
        stopAppium();
    }

    public static void startAppium(){
        System.out.println("About to start appium service locally");
        appiumService = AppiumDriverLocalService.buildDefaultService();
        appiumService.start();
    }

    public static void stopAppium(){
        System.out.println("About to stop appium service running locally");
        appiumService.stop();
    }
}

Leave a Reply

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