How to install app in Android device with Java & ADB command in Appium?

If you remember in the previous tutorial we discussed triggering Appium server with Java programming, which is a good solution when we have to run our automation tests cases through CI/CD configuration. Sometimes we don’t have clear information whether the testable app is installed on the device or not, so in such case, we try to install app (basically .apk file) through external sources. It’s good we have access to the play store, but sometimes emulator doesn’t even have play store.

In this tutorial, we will learn to install app in a testable device with a Java program and through ADS commands. For all that, we need to have Appium server up and running.

Before we jump to today’s agenda, I have some article suggestions for you.

There can be many ways to install the app in your test android device, but the easiest way is directly installing from Google Play store. Now the question comes, if we can install through play store then why are we reading this article.

This article is going to help you in the following situations:

  1. When you do not have Play store in Android emulator
  2. Sometimes you need an app at runtime
  3. During test execution at the server using CI/CD pipeline

Install app for testing

Pre-Requisites

  • Appium Server should be Up and running
  • The programming environment is ready in IDE
  • APK file for respective test app should be available in local/server directory

We are going to discuss two techniques to install app in your android devices, one through ADB commands and another through java program. Let’s discuss each one of them separately.

Technique# 1: Installing Test App through ADB Commands

I expect you are ready with the above pre-requisites. Next, we just need to run the ADB command containing the path to apk file. Use the following command to install app in your android device.

C:/> adb install <path_to_apk_file>

See image below:

Install app thru adb cmd command

Technique# 2: Installing Test App through a Java program

This technique might help you when you want to install app at runtime during execution at the server. Again I would ask you to check the pre-requisites above.

You just need to add the following capability for installing the app at runtime to your Android devices.

DesiredCapabilities cap = new DesiredCapabilities();

cap.setCapability("app", "path_to_apk_file");

Following is the sample program which installs the Flipkart app at runtime and launches the same app in the mobile device.

package AppiumPractice;

import java.io.File;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;

public class InstallApp {
	
	static AppiumDriver<MobileElement> driver;

	public static void main(String[] args) throws Exception {		
		installAppWithAppium();
	}
	
	public static void installAppWithAppium() throws Exception{
		
		File rootPath = new File(System.getProperty("user.dir"));
		File appFolder = new File(rootPath, "/Apps/Flipkar/");
		File app = new File(appFolder, "Flipkart App.apk");

		DesiredCapabilities cap = new DesiredCapabilities();
		cap.setCapability("deviceName", "vivo 1818");
		cap.setCapability("udid", "d7206a48");
		cap.setCapability("platformName", "Android");
		cap.setCapability("platformVersion", "9");
		cap.setCapability("app", app.getAbsolutePath());
		cap.setCapability("appPackage", "com.flipkart.android");
		cap.setCapability("appActivity", "com.flipkart.android.activity.HomeFragmentHolderActivity");

		driver = new AndroidDriver<MobileElement>(new URL("https://127.0.0.1:4723/wd/hub"), cap);
		driver.manage().timeouts().implicitlyWait(80, TimeUnit.SECONDS);
		Thread.sleep(10000);
		driver.quit();		
	}
}

Hope this article will help you in many ways. If you find any difficulties, feel free to write your problems in the comment section below. For more updates do like our FB page and join our FB group.

Join Inviul fb group

Leave a Reply