Create Appium script for Android Mobile App

In this Post, we are going to create our first Appium script for mobile app and we will be running it on Android real device. In our previous posts, we have already covered the installation and configuration part that you have to do before creating Appium script. If you haven’t done it yet then please follow the links provided under Pre-requisite section.

Pre-requisite

Scenario

Here is the scenario that we are going to cover :
1.   Launch Calculator App on Android real device
2.   Multiple two numbers
3.  Print result
4.  Close Calculator App

NOTE : First, we need following 2 important information about the App that we will be going to automate :

  • Main Activity class Name
  • Package name

We have to mention the Activity and Package Name in the Desired Capabilities of our script, so that Appium could recognize that which Page(Activity) it has to launch on device. We can simply get this information from “apk Info” App. You can download this app from Google play store on your android device. When you open this app, it will display all the installed apps of your device. Just look for calculator app and then tap on it to get the Activity and Package Name. Refer screen shot below :

Apk-info

Program


import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

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

public class CalculatorTest {
	public AppiumDriver<MobileElement> driver = null;

	@BeforeTest
	public void setupstart() throws MalformedURLException {
		DesiredCapabilities capabilities = DesiredCapabilities.android();
		capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "Appium");
		capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
		capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "4100b79b459381f7");
		capabilities.setCapability("appPackage", "com.sec.android.app.popupcalculator");
		capabilities.setCapability("appActivity", "Calculator");
		driver = new AndroidDriver<MobileElement>(new URL("http://localhost:4723/wd/hub"), capabilities);
	}

	@Test
	public void calcTest1() throws Exception {
		driver.findElement(By.xpath("//android.widget.Button[@content-desc='4']")).click();
		driver.findElement(By.xpath("//android.widget.Button[@content-desc='Multiplication']")).click();
		driver.findElement(By.xpath("//android.widget.Button[@content-desc='3']")).click();
		driver.findElement(By.xpath("//android.widget.Button[@content-desc='Equal']")).click();
		String result = driver.findElement(By.className("android.widget.EditText")).getText();
		System.out.println("Result : " + result);
	}

	@AfterTest
	public void tearDown() {
		driver.quit();
	}
}

In the above program, desired capabilities are mentioned which helps Appium to understand that which browser it has to invoke and on which device etc.
Desired capabilities are basically a set of keys and values which goes to the Appium server to tell that what kind of automation session user interested in starting up. There are various capabilities which can modify the behavior of the server during automation. You can refer the server capabilities from Appium Doc.

Below, We have explained the Appium server capabilities that have been used in our script.

1. Automaton name : Appium

capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "appium");

2. Platform Name : Android
As we have created this script for android, so we have to tell Appium about the platform where we actually want to run our script. For Iphone, it would be iOS.

capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");

3. Device Name : {Unique Device ID}
We have to tell the Device name to Appium server so that it could recognize the device before launching the Mobile App. You will get this uniqueID after running the “adb devices” command in command Prompt.

capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "4100b79b459381f7");

4. In order launch Application on device, We have to mention App Package and Activity name in Desired Capabilities to allow the script to open the application under test.

capabilities.setCapability("appPackage", "com.sec.android.app.popupcalculator");
capabilities.setCapability("appActivity", "Calculator");

5. Here, we have to provide the IP address and port number that we have used while starting the Appium server

driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);

If you find the information provided above is useful, please don’t forget to like us on Facebook, you can also leave the comment.

Leave a Reply

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