Automating a Mobile Application using Appium (Part 2)


automating a mobile application in Appium

Before beginning with the next step towards learning Appium, do make sure that you have read my previous blog on Appium Setup in Windows. Only after you have made all the installations described in that blog, you can start running your test scripts without any hindrance.

My blog on Automating a Mobile Application using Appium, describes a very basic Login flow of a demo application.

The simple steps are as follows: User (the one who is already registered) selects the Country to which he belongs, enters his registered Mobile Number and his name, all on one screen after which the entered Mobile Number is validated. And after successful validation, user is re-directed to a screen where he is asked to enter his password.

Please Note: I have only discussed the happy flow here, i.e. using just valid values.


Before beginning make sure Developer options as well as USB debugging mode is enabled for your android device.

There are some other set of things you need to do:-

1. Following list of parameters of the Android device as well as the application under test which will be needed in our test script should be noted down:

a) Device Name: Run command “adb devices” in command prompt. You’ll get the device name there.

b) OS Version: Android version of your device.

c) App Activity Name & Package Name: Download APK Info application in your Android device. The APK path section will give the package name (e.g. : com.example.android.contactmanager) and the launcher activity can be obtained from the Activities section here. (The very first one in the list!).

2. Start Appium Node Server. It takes some time for it to start. When you get this, consider it as started:


3. Locating application elements using UI Automator Viewer.

UI Automator Viewer (in the tools folder of SDK) helps us in locating elements in our application, so as to first identify them and perform some action on them subsequently. It gives certain properties of the application components, which we can be further used to create xpaths and can be used in our automation test scripts. (You will find “uiautomatorviewer.bat” file in tools folder of SDK).

In UI Automator Viewer tool, click on Device Screenshot image button while the application to be tested is open in your mobile device. This shows your application’s UI in UI Automator Viewer, as shown below.


The top right hand side part explains how elements are arranged. The bottom right hand side part shows the properties of the selected elements along with their values.


a) As can be seen, the Submit button has a unique class -> “android.widget.Button” having index=8. So, the xpath for the Submit button would be:


b) Taking another example of the combo box that pops up on the click of the field which reads “SELECT THE COUNTRY YOU LIVE IN”:

Two things need to be considered here:

1. Clicking on the field itself, which opens a list of countries in turn:


Here the field has class “android.widget.TextView”, which is not a unique one (A few other elements have the same class). So in order to create a unique xpath I need some other property that makes this element unique. I can identify the element using the resource-id property. So, the xpath for the field will be:


2. Selecting one country from the list


Now, I wish to select country “India” from the list. In this case each country has a unique text attribute. So, we can uniquely identify this particular element just by the name (i.e. text= “India”)


Below is the code for launching the demo app and performing Login operation:

package Android;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities; //Used to set device properties:device name, platform, version, app package name, activity name, etc. It has various methods eg. setCapability()-> used to set the device name, platform version, platform name, absolute path of the app under test (the .apk file of the app(Android) under test), app Activity (in Android) and appPackage(java).
import org.testng.annotations.AfterTest; //@AfterTest annotated method will be executed when all @Test annotated methods completed its execution
import org.testng.annotations.BeforeTest; //@Before Test annotated method will be executed before any @Test method
import org.testng.annotations.Test; //@Test annotation describes method as a test method or part of your test

public class DemoApp{
AndroidDriver driver;
public void SplashScreen() throws MalformedURLException, InterruptedException {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "ZY2234ZDFF"); //obtained from the command "adb devices"
capabilities.setCapability("browserName", "Android");
capabilities.setCapability(CapabilityType.VERSION, "6.0");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("appPackage", "<App package name>");
capabilities.setCapability("appActivity", "<App activity name>");
driver = new AndroidDriver(new URL(""), capabilities);
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

public void Login(){
driver.findElement(By.xpath("//android.widget.TextView[contains(@resource-id,'mas_account_verification_country')]")).click(); //Open drop down country menu
driver.findElement(By.name("India")).click(); //Select one country
driver.findElement(By.xpath("//android.widget.EditText[contains(@resource-id, 'et_account_verification_phone_number')]")).sendKeys("9999999999"); //Enter Mobile No
driver.hideKeyboard(); //Hide keyboard
driver.findElement(By.name("Enter your name")).sendKeys("Parul"); //Enter name
driver.hideKeyboard(); //Hide keyboard
driver.findElement(By.className("android.widget.Button")).click(); //Click on submit button
driver.findElement(By.className("android.widget.EditText")).sendKeys("123456"); //Enter password in next screen
driver.hideKeyboard(); //Hide Keyboard, since keyboard hides the Submit button in this case
driver.findElement(By.xpath("//android.widget.Button[@index='1']")).click(); //Click on submit button

public void end()
driver.quit(); //Close application


This launches the demo application on the android device, validates the entered Mobile Number (according to the country selected as well as length), enters the password in the next screen and on verifying the above sent data logs in the user successfully and exits from the app immediately.

Before running the above test script from Eclipse, you need to start Appium server first.

Please note that, the same process will be followed each time you are testing your application, whether it be performing an end-to-end test, or to just test a particular flow (say, Registration). The core lies in detecting the elements right and in a unique manner.

Apart from this, there are a whole lot of other things which can also be tested while automating an Android application, say picking an image from gallery/clicking an image from a custom camera, scrolling to a particular element, handling of alert dialogue, swipe vertical and horizontal, taking screenshot etc. This blog would help you take one step towards achieving it all and much more!

Thanks for reading. 🙂

You might also like