Launching Native App in your Android device using Appium


You already know what are Desired Capabilities and how to launch a web browser from previous article. Now lets launch a native app. You also know how to create an AVD, if not then take a look at this article. Get the device name using the command ‘adb devices‘. Here, we are going to take an example of the Android calculator app.

Things to do:
Launch emulator
Launch Appium server – type ‘appium
Get the device name – type ‘adb devices
Get the package name and activity name of calculator app – refer article
Set the capabilities (package name and activity name) – refer article

Xpath001

Now, create a class in your project, let’s say ‘LaunchNativeApp‘ and paste the below code in that class

private AndroidDriver driver;

@BeforeClass
public void setUp() throws MalformedURLException {
DesiredCapabilities caps = new DesiredCapabilities();
//set the platform version
caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, “4.4.2“);
caps.setCapability(MobileCapabilityType.PLATFORM_NAME, “Android“);
caps.setCapability(MobileCapabilityType.DEVICE_NAME, “Android emulator“);
//set the emulator name
caps.setCapability(“avd”, “CharanAvd“);
caps.setCapability(MobileCapabilityType.APP_PACKAGE,
com.android.calculator2“);
caps.setCapability(MobileCapabilityType.APP_ACTIVITY,
com.android.calculator2.Calculator“);
driver = new AndroidDriver(new URL(“http://127.0.0.1:4723/wd/hub”),
caps);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}

@Test
public void calculatorAddition() {
WebElement nine = driver.findElement(By.name(“9”));
nine.click();
WebElement plus = driver.findElement(By.name(“+”));
plus.click();
WebElement seven = driver.findElement(By.name(“7”));
seven.click();
WebElement equalTo = driver.findElementByAccessibilityId(“equals”);
equalTo.click();
}

@AfterClass
public void tearDown() {
driver.closeApp();
}

Hope, it worked. Your code launches calculator app and perform an operation. Notice here, I was using ‘AccessibilityId‘ locator for ‘=’ button, instead of ‘name’ or ‘ID’. You can use any locator. Try to locate other buttons as well.

Explanation:
You know, we need to set capabilities to start a session. Here, I’m using an emulator, so the device name was my emulator’s. My platform version was 4.4.2. Since i am using calculator app, it’s package name is ‘com.android.calculator2‘ and activity name is ‘com.android.calculator2.Calculator‘.

Sending a post request to server saying, CharanAvd having version 4.4.2 wants to launch an application having package name as ‘com.android.calculator2’ and activity name as ‘com.android.calculator2.Calculator’. Then UIAutomator/Selendroid/Appium will talk to bootstrap.jar which is running your device to perform an action. Then bootstrap.jar acts a TCP server establish a connection.

Hope this is clear. You can download the code from here.

Advertisements

One thought on “Launching Native App in your Android device using Appium

Leave a Reply

Please log in using one of these methods to post your comment:

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