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 buildService(), usingPort() and start() methods in AppiumServiceBuilder class. This will start Appium in specified port for example, 4729.
appiumServiceBuilder = new AppiumServiceBuilder();
appiumServiceBuilder.usingPort(4729);
appiumService = AppiumDriverLocalService.buildService(appiumServiceBuilder);
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/iOS 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 io.appium.java_client.service.local.AppiumServiceBuilder;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.net.MalformedURLException;
import java.net.URL;
public class StartAndStopAppiumProgrammaticallyUsingServiceBuilder {
public static AppiumDriverLocalService appiumService;
public static AppiumServiceBuilder appiumServiceBuilder;
public static void main(String[] args) throws MalformedURLException {
startAppiumUsingServiceBuilder();
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "****");
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:4729/wd/hub");
AndroidDriver<AndroidElement> driver = new AndroidDriver<>(url, capabilities);
driver.quit();
stopAppium();
}
public static void startAppiumUsingServiceBuilder(){
System.out.println("About to start appium service locally with different arguments");
appiumServiceBuilder = new AppiumServiceBuilder();
appiumServiceBuilder.usingPort(4729);
appiumService = AppiumDriverLocalService.buildService(appiumServiceBuilder);
appiumService.start();
}
public static void stopAppium(){
System.out.println("About to stop appium service running locally");
appiumService.stop();
}
}