January 2011

Test Android apk file with Robotium

In our last tutorials we have created AndroidCalculator and tested it through eclipse using AndroidCalculatorTest project as Thanks to Robotium as it provides multiple ways to test Android applications. So if you are not familiar with basic coding (which is prerequisite of White Box Testing) then Robotium provides the way to test apk file as well.
We will test the AndroidCalculator apk file, initially we will install AndroidCalculator apk file on device then we will create project on eclipse to test it.

Note:we should have android working environment to work on, you can find tutorial to Set up Android Working Environment.

1. Install APK

We will install android apk file on Android simulator, real device can also be used but its not on scope of our tutorial. Before installing apk file we must make sure that apk file is signed in debug mode, if not follow next step. If apk file has debug mode signatures then skip this step and move on the Load Emulator.

Sign Apk file in debug mode

The apk file must have the same certificate signature that your test project has. The signature will identify the author of the android application. Signature means it contains the information like first name and last name of the developer, Name of the organizational unit, organization, city, state, two-letter country code. Standard tools like Keytool and Jarsigner are used to generate keys and sign applications. For more help visit: http://developer.android.com/guide/publishing/app-signing.html

Important Steps:
* If you know the certificate signature then you need to use the same signature in your test project
* If you do not know the certificate signature then you need to delete the certificate signature and you should use the same android debug key signature in both the application and the test project
* If the application is unsigned then you need to sign the application apk with the android debug key

If the application is signed then you can use the following drag and drop java program to resign it: http://www.troido.de/re-sign.jar
Or
* Un-zip the apk file
* Delete the META-¬INF folder
* Re‐zip the apk file
* In Dos prompt/Terminal write following commands

> arsigner -keystore ~/.android/debug.keystore -storepass android -keypass android ApplicationToTest.apk androiddebugkey
> zipalign 4 ApplicationToTest.apk TempApplicationToTest.apk

Then rename TempApplicationToTest.apk to ApplicationToTest.apk. If it is an unsigned application then, In Dos prompt/Terminal write following commands

> jarsigner -keystore ~/.android/debug.keystore -storepass android -keypass android ApplicationToTest.apk androiddebugkey
> zipalign 4 ApplicationToTest.apk TempApplicationToTest.apk

Then rename TempApplicationToTest.apk to ApplicationToTest.apk For more help visit: http://developer.android.com/guide/publishing/app-signing.html

Load Emulator

Load command prompt (windows user) or Terminal (Linux users) and write following command to run emulator

emulator -avd

here the Device Name is our AVD (Android Virtual Device) we created in Set up Android Working Environment, it will load the emulator give it some time to load.
If it shows some error most probably the PATH would not be set for Android SDK. Follow the below Set up Android SDK path if not working, you can skip this step if your emulator is working.

Set Up Android SDK Path

Follow the section suits your OS (Operating System) to set SDK path

Linux (ubuntu)
Load terminal and write

echo $PATH

(it will print value of PATH, if Android SDK path is not visible move on next step to add path). To set path enter following command

sudo gedit /etc/bash.bashrc (it will ask password, enter the password)

It will load bash.bashrc file into gedit (text editor) and at end of file add following lines into file and save it.

Note: it is showing the path for all tools necessary for android, based on my current system directory, you need to replace with your own path where Android SDK resides.

export PATH=${PATH}:/home/naveed/android-sdk/
export PATH=${PATH}:/home/naveed/android-sdk/platforms/android-8/tools/
export PATH=${PATH}:/home/naveed/android-sdk/tools/
export PATH=${PATH}:/home/naveed/android-sdk/platform-tools/

now reload the Termial and run emulator -avd Device command again it will load emulator.

Windows
Load Command Prompt and write

echo $PATH

(it will print value of PATH, if Android SDK path is not visible move on next step to set path). To set path enter following commands one by one

sett PATH=${PATH}:c:/naveed/android-sdk/
set PATH=${PATH}:c:/naveed/android-sdk/platforms/android-8/tools/
set PATH=${PATH}:c:/naveed/android-sdk/tools
set PATH=${PATH}:c:/naveed/android-sdk/platform-tools/

Note: it is showing the path for all tools necessary for android, based on my current system directory, you need to replace with your own path where Android SDK resides.

now reload the Command Prompt and run

emulator -avd

Now it will load emulator. After the emulator is working, load another instance of command prompt/terminal (based on your os), and write following command to install AndroidCalculator apk on emulator

adb install /AndroidCalculator.apk

the is showing the directory where AndroidCalculator.apk is located, in may case it was

adb install /home/naveed/AndroidCalculator.apk

as apk is at my home directory ( I am using ubuntu). You need to enter your own path where apk resides. It will successfully install apk file and show success message you can load the AndroidCalculator application and try running it.

2. Create Test Project

Click on File menu, select New and click on the Others,
From New window, Drag down to Android option, expand it, and select Android Test Project and Click on Next.

Note: As we are going to create test project to test AndroidCalculator apk so we must be care full with the Package name, our package name should be with the parent node of our apk application’s package like our package name of our AndoridCalculator was com.calculator, so our test project should be with in the parent node (com), like we will put the package name for test project as com.testcalculator or we can also create package like com.calculator.testapk.

So please be careful while entering the package name.
If you do not know the exact package name of AUT (Application Under Test) then type following commands in the DOS prompt /Terminal after launching emulator

> adb install testapplication.apk
> adb logcat

Run the application once and you will get the exact package name in logcat window
From New Android Project Window, enter Test Project Name as ‘AndroidCalculatorTestApk’, Check Android 2.2 from Build Target section, Application Name ‘AndroidCalculatorTestApk’, Package Name ‘com.testcalculator’, ‘8’ as Min SDK Version and click on Finish.

3. Create Test Class

Right click on com.testcalculator package, from New click on Class to add class. We will write our code to test AndroidCalculator apk. Enter class name TestApk (you can put and any name suits you) and click on Finish.

4. Code Test Class

Enter following code into new created TestApk file and save it.

package com.testcalculator;

import com.jayway.android.robotium.solo.Solo;
import android.test.ActivityInstrumentationTestCase2;

@SuppressWarnings(“unchecked”)
public class TestApk extends ActivityInstrumentationTestCase2{

private static final String = TARGET_PACKAGE_ID=”com.calculator”;
private static final String = LAUNCHER_ACTIVITY_FULL_CLASSNAME=”com.calculator.Main”;
private static ClasslauncherActivityClass;

static{

try
{
launcherActivityClass=Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
} catch (ClassNotFoundException e){
throw new RuntimeException(e);
}
}

public TestApk()throws ClassNotFoundException{
super(TARGET_PACKAGE_ID,launcherActivityClass);
}

private Solo solo;

@Override
protected void setUp() throws Exception
{
solo = new Solo(getInstrumentation(),getActivity());
}

public void testDisplayBlackBox() {

//Enter any interger/decimal value for first editfield, we are writting 10
solo.enterText(0, “10”);

//Enter any interger/decimal value for first editfield, we are writting 20
solo.enterText(1, “20”);

//Click on Multiply button
solo.clickOnButton(“Multiply”);

//Verify that resultant of 10 x 20
assertTrue(solo.searchText(“200”));
}

@Override
public void tearDown() throws Exception
{
try
{
solo.finalize();
}
catch (Throwable e)
{
e.printStackTrace();
}

getActivity().finish();
super.tearDown();
}
}

5. Add Robotium jar

We need to reference the Robotium jar to our project. Right click on project select Build Path, and then click on Configure Build Path option. On Properties window click on Libraries tab and add Robotium jar into project.

We can download Robotium jar from http://code.google.com/p/robotium/downloads/list

6.Targeting AndroidManifest.xml to apk

From project explorer window open AndroidManifest.xml file. Our AndroidManifest.xml will look like

7. Run your test

Its time to run test, now right click on AndroidCalculatorTestApk project and Run As AndroidJUnit Test. Wait for some time, it will load AndroidCalculator.apk (installed on emulator)

* Enter the first & second values into editfields
* Click on Multiply button
* Verify their multiply value.

After complete verification it will show the report like below.


We can also run our test using command prompt/termial by follwoing simple steps

* Write following command to install AndroidCalculator apk on emulator
> adb install /AndroidCalculator.apk

* Write following command to install AndroidCalculatorTest apk on emulator
> adb install /AndroidCalculatorTest.apk

* Run the test cases:
> adb shell am instrument ‐w com.testcalculator/android.test.InstrumentationTestRunner

Test Android Sample App with Robotium

1. Create Test Project
To test an Android application using Robotium, we need to create a test project with in the package (com.calculator) of specific project. We have already created our test project in last section, hope you got viewing below screen,

We will move on to design our logic to test AndroidCaculator. We need to create test case class where we will write code to test AndroidCalculator’s main class (Main.java).

2. Create Test Case
In test project from project explorer window right click on com.calculator.test select New then others. On New window expand Java and then expand Junit category and select Junit Test Case and click on Next.


On New Junit Test Case screen, most of the options will be automatically filled as we have already created test project (AndroidCalculatorTest) with project (AndroidCalculator). We need to enter the Name of Test case, which I will enter TestMain, as I am going to test (main.java) of AndroidCalculator project. On next section check Setup(), tearDown() & Constructor options and click on Finish.


A new test case by the name of TestMain will be created into com.calculator.test package of my test project (AndroidCaculatorTest).


3. Add Robotium Jar
We need to reference the Robotium jar to our project.

Right click on project select Build Path, and then click on Configure Build Path option. On Properties window click on Libraries tab and add Robotium jar into project.

You can download Robotium jar from http://code.google.com/p/robotium/downloads/list


4. Write Test Case Code
In our create test case we will access the contents of AndroidCalculator and do followings,

a. Call/Access first & second input controls (EditFields)
b. Enter values of our own choice
c. Access & Click on Multiply button
d. Put assert to verify their multiplication result into result field.

And add following code into TestMain.java class and save it.


package com.calculator.test;

import java.util.ArrayList;

import android.app.Activity;

import android.test.ActivityInstrumentationTestCase2;

import android.widget.EditText;

import android.widget.TextView;

import com.calculator.Main;

import com.calculator.R;

import com.jayway.android.robotium.solo.Solo;

public class TestMain extends ActivityInstrumentationTestCase2

{

private Solo solo;

public TestMain() {

super(“com.calculator”, Main.class);

}

@Override

protected void setUp() throws Exception {

super.setUp();

solo = new Solo(getInstrumentation(), getActivity());

}

@Override

protected void tearDown() throws Exception{

try {

solo.finalize();

} catch (Throwable e) {

e.printStackTrace();

}

getActivity().finish();

super.tearDown();

}

public void testDisplayBlackBox() {

//Enter any integer/decimal value, we will enter 10 in first editfield

solo.enterText(0, “10”);

//Enter any integer/decimal value, we will enter 20 in second editfield

solo.enterText(1, “20”);

//Click on Multiply button

solo.clickOnButton(“Multiply”);

//Verify that resultant of 10 x 20

assertTrue(solo.searchText(“200”));

}

public void testDisplayWhiteBox() {

//Defining our own values to multiply

float firstNumber = 10;

float secondNumber = 20;

float resutl = firstNumber * secondNumber ;

//Access First value (editfiled) and putting firstNumber value in it

EditText FirsteditText = (EditText) solo.getView(R.id.EditText01);

solo.enterText(FirsteditText, String.valueOf(firstNumber));

//Access Second value (editfiled) and putting SecondNumber value in it

EditText SecondeditText = (EditText) solo.getView(R.id.EditText02);

solo.enterText(SecondeditText, String.valueOf(secondNumber));

//Click on Multiply button

solo.clickOnButton(“Multiply”);

assertTrue(solo.searchText(String.valueOf(resutl)));

TextView outputField = (TextView) solo.getView(R.id.TextView01);

ArrayList currentTextViews = this.solo.getCurrentTextViews(outputField);

assertFalse(currentTextViews.isEmpty());

TextView output = (TextView) currentTextViews.get(0);

//Assert to verify result with visible value

assertEquals(String.valueOf(resutl), output.getText().toString());

}

}

5. Run Test Case
Now as we are almost done so now its time to run our test case.

Right click on TestMain.java file select Run As option and then click on Android Junit Test. It will start running Junit test.

Select the emulator or device to run the test (we will be using Android default emulator) , and wait for a while to see the magic of Robotium.

If things are going fine

1. Emulator will load, Unlock it.

2. AndroidCalculator application will load

3. It will automatically enter first & second values in First and Second EditField, and click on Multiply button (you can see all this happening as record & play scripts)

4. After successfully execution it will show green bar showing the successful execution and all results are passed.

Download AndroidCalculatorTest project code. In next section we will test android apk file with Robotium.

Create Android Sample Application

After setting up Android working environment we will start designing our own sample application, which we will test using Robotium in next section.

Our sample application would be a simple calculator to multiply two integer/decimal values. It will take two inputs and on clicking ‘Multiply’ it will show their multiply result.

For simplicity steps are categorized as,

1. Create Project
Click on File menu, select New and click on the Others,
from New window, Drag down to Android option, expand it, and select Android Project and Click on Next.


From New Android Project Window, enter Project Name as ‘AndroidCalculator’, From Contents section select ‘Create New Project in workspace’, Check Android 2.2 from Build Target section, Application Name ‘AndroidCalculator’, Package Name ‘com.calculator’, Create Activity ‘Main’ and ‘8’ as Min SDK Version

Note: you can enter any other options best suits to your need.

Click on Next and it will load New Android Project window eclipse offer you to create project to test your project, at this time we will avail it to test ‘AndroidCalculator’ we made in last step. We can skip this part by un-checking Create a Test Project option, if you want to manually create test project later, we will create test project right away.

2. Create Test Project
Check Create a Test Project and it will automatically fill rest of fields based on the last made project (AndroidCalculator), and click on Finish.


Note: As our current focus is to design sample project so for now, we will just create test project and in next section we will be working on this new created project to test AndroidCalculator.

After successfully creating projects, our Project explorer screen should look like,

Now two projects are created, we will simply work on first project to design our sample calculator application.

3. Understand Project Architecture
Expand the src directory then expand com.calculator directory, Main.java file contains application logic. In rec directory we can define application’s UI interface. In Main.xml we can put controls on application interface and in string.xml we can define their string values,which would be visible on UI.

We would not get into details, as its not in our scope so far.

4. Design Layout
Get the code for main.xml and string.xml from Android Calculator Code

5. Design Application Logic
In Main.java enter following code and save it.Our application is designed and its time to run it.Right click on project select Run As and then select Android Application & and wait for while.It will load Android simulator, you need to wait for some time, it will launch application itself.

Our simple multiply calculator is ready, enter some integer/decimal values and click on Multiply, it will show the result above Click button.

Next: In next step we will work on AndroidCaculatorTest project to test that simple calculator. Download Android Calculator Code


Setup Android Working Enviroment

To work on android we need to setup working environment. Before setting up android environment we need to fulfill some basics like

* JDK must be installed (to install http://www.oracle.com/technetwork/java/javase/downloads/index.html)
* Eclipse for java must be installed (http://www.eclipse.org/downloads/)

We are expecting that eclipse is working fine.

1. Download Android SDK
It includes only the core Standard development Kit (SDK) Tools, which will help us in developing Android Applications.
Download Android SDK from http://developer.android.com/sdk/index.html
After downloading .zip or .tgz package (instead of the SDK installer), unpack it to a safe location on your machine.

We will be using it later on in coming steps.

2. Install Android development Tools (ADT)
In Eclipse From Help menu click on Install New Software option


Click on Add button and on Add Site window fill following url to download Android ADT, you can enter any name in Name field, and click on Ok button.


Eclipse will search for the available tools and show their list.


Select all tools and click on Next. It will start checking the things and will show list of tools which will be installed.


Click on Next button and after a license verification it will start downloading and may take some time depending upon the speed of Internet. After successfully installing it will ask to restart eclipse. Restart it.

3. Adding SDK Location
From Window menu click on Preferences there would be Android option visible (which is also assurance that android ADT is installed 🙂 ). Here we need to tell Eclipse where the Android SDK is located. So click on Android from list, and then browse it to the SDK unzipped directory and click on Ok.


4. Install SDK Latest Version
After that come up on Android SDK and AVD Manager from the Window menu. Select available packages and select the latest version of the SDK, in my case I will select followings, you can select based on your own choice and click on Install Selected to complete the installation, and restart eclipse after it.


During installation It would be looking like this.


5. Set Up Device
As we are almost done, last step is we need to set up device to work, real device can be attached, but we will be using simulator for the scope of our work. From eclipse interface click on this icon from top left side of IDE.

Select the option Virtual Devices and click on New.

On Android Virtual Device screen fill the followings and click on Create AVD.


It will show the device added in Android SDK and AVD manager screen

Now our setup is complete and we are ready to develop android applications. Click to Design Android Sample App