Mobile Automation

How to get all TextViews on screen using Robotium

static void printAllTextViews() {
vTextViewsList = null;
vTextViewsList = solo.getCurrentViews(TextView.class);
print(“Total TextViews:” + vTextViewsList.size());
vIndex = 0;
for (TextView vTextView : vTextViewsList) {
if (vTextView.getVisibility() == View.VISIBLE) {
print(“TextView ID: “
+ vTextView.getId()
+ “Index: “
+ vIndex
+ ” Tag: “
+ (vTextView.getTag() != null ? vTextView.getTag()
.toString() : “null”) + ” Value: “
+ vTextView.getText().toString() + ” Visibility: “
+ vTextView.getVisibility() + ” View String: “
+ vTextView.toString());
vIndex++;
}
}

}

How to Get all Views on screen using Robotium

static void printAllViews() {
ArrayList allViews = solo.getCurrentViews();
print(“Total Views:” + allViews.size());
for (View vView : allViews) {
if (vView.getVisibility() == View.VISIBLE) {
print(“View : “ + vView.toString() + “View ID: “
+ vView.getId() + ” Value:”
+ vView.getClass().getName().toString()
+ ” Visibility:” + vView.getVisibility());
}
}

}

How to get All TextViews in each row of a ListView using Robotium

public void printAllTextViewsByRowIntoListView(){
vListView = solo.getCurrentViews(ListView.class).get(0);
        if (vListView.getCount() > 0) 
        {
          for (int i = 1; i <= vListView.getLastVisiblePosition(); i++) 
         
          vListRow = vListView.getChildAt(i);
          vTextViewsInListRow = solo.getCurrentViews(TextView.class, vListRow); 
          print(“Row #”+i+” contents “);
          for (int j = 0; j < vTextViewsInListRow.size(); j++) 
          { 
          print(vTextViewsInListRow.get(j).getText().toString());
          }
          print(“………”);
          }
        }
          else print(“No contents available”);
}

Understanding Android Hierarchyviewer Tool

Using Android SDK Hierarchy Viewer

Automating Android app calls for many limitations of different types. During writing test cases using Robotium we have two choices

  • If AUT source code is available, we know the contents used within the application, and it’s pretty easy to work with these contents using Robotium
  • If we don’t have AUT (Application Under Test) code, there is only apk file available, we neither have much idea about the contents used within the app nor even sure about the properties bind with contents and their values, so we have to stay with situation with some assumptions
Android SDK has made software Testers’ life easy by providing a useful tool “Hierarchy Viewer”. It shows the hierarchical view of all contents used within the running screen of app along with their details (id, Name, Positions and all other Properties with their values).  
Note: Before starting, we must insure that Android Environment is properly setup and working fine. Please setup the environment by clicking here.

Run Hierarchy Viewer

To run Hierarchy Viewer, follow these steps:

  • Launch an emulator.

-To preserve security, Hierarchy Viewer can only connect to devices running a developer version of the Android system.

  • If you have not done so already, install the application you want to work with. To make things simple we would be usingNotes App”, it is one of the sample apps bundled in Android SDK with source code.
  • Run the Notes application, and ensure that its UI is visible.
  • From a terminal, launch hierarchyviewer  or double click on hierarchyviewer.h from the /tools/ directory
  • The first window you see displays a list of devices and emulators. To expand the list of Activity objects for a device or emulator, click the arrow on the left. This displays a list of the Activity objects whose UI is currently visible on the device or emulator. The objects are listed by their Android component name. The list includes both your application Activity and system Activity objects.


Following is figure showing packages of all app running on device, select package of Notes app and click on Load View Hierarchy button

You can now look at its view hierarchy using the View Hierarchy window, or look at a magnified image of the UI using the Pixel Perfect window.

We can have more technical details of Hierarchy Viewer on Android SDK website .
On selecting any of the content it will load its details, Notes is a simple application, in case of complex applications, we can understand power of hierarchy viewer more.

UI Identification of Contents on AUT

Using Notes app one can create and manage multiple notes easily. App contains following screens

  • NotesList screen to show list of created  notes
  • NoteEditor screen to create and edit notes
  • TitleEditor screen to edit title of specific note

Each screen contains some contents to perform specific functionality. These contents could me TestView, EditView, ImageView, button etc..

To test Notes app using Robotium we need to know about contents in details, then we can perform actions on contents. Going through app from device/emulator only shows visible contents, but if some contents are hidden to arrange/organize layouts device will not show it.

hierarchy viewer tool can be used to cover this gap, it shows all contents used on screen in hierarchy.

We will go through each screen of app from device then hierarchy viewer view to get idea about contents used

Empty NoteList Screen
NoteList screen shows following contents on device

  • imageview to show “Notes” icon
  • textview to show text “Notes”
  • imageview to show “Create Note” icon
hierarchy viewer will show following contents hierarchy section of above screen


it is showing following contents

  • textview to show “Notes”
  • textview to show sub title which is hidden
  • imageview to show “Notes” icon
  • imagebutton to show “Create Notes” icon
  • Button with text “New Note” which is also hidden

Contents in bold are basically made hidden, that’s why these are not visible on device, and we don’t even sure about the properties bind with all of them.

On hierarchy view each block represent a content, on tapping any content will load its position view, layout view and all parameters with their values including name, id, position, style, size etc.






NoteList with some notes
When there are some notes added, the notelist screen would look like

  • imageview to show “Notes” icon
  • textview to show text “Notes” text
  • imageview to show “Create Note” icon
  • textview to show “Monthly Notes” text
  • textview to show “Weekly Lists”text
  • textview to show “Today List” text

hierarchy viewer will show following contents hierarchy of above screen
It will show following contents

  • textview to show “Notes” text
  • textview to show sub title which is hidden
  • imageview to show “Notes” icon
  • imagebutton to show “Create Notes” icon
  • Button with text “New Note” which is also hidden
  • listview to show following notes list
  • textview to show “Monthly Notes” text
  • textview to show “Weekly List” text
  • textview to show “Today List” text

Contents in bold are basically made hidden, that’s why these are not visible on device, and we don’t even sure about the properties bind with all of them.

On hierarchy view each block represent a content, on tapping any content will load its position view, layout view and all parameters with their values including name, id, position, style, size etc.

NoteEditor Screen
NoteEditor screen shows following contents on device

It gives following idea about contents

  • imageview to show “Notes” icon
  • textview to show text “Edit: Weekly List” text
  • imageview to show “Create Note” icon
  • imageview to show “Delete Note” icon
  • textview to show “Monthly Notes” text
  • textview to show “Weekly List” text
  • textview to show “1. Task one” text
  • textview to show “2. Task two” text
  • textview to show “3. Task three” text
  • textview to show “4. Task four” text
  • textview to show “5. Task five” text

hierarchy viewer will show following contents hierarchy of above screen,

It gives following idea about contents

  • imageview to show “Notes” icon
  • textview to show text “Edit: Weekly List” text
  • textview is hidden for space
  • imageview to show “Create Note” icon
  • Button with text “Save” which is also hidden
  • imageview to show “Delete Note” icon
  • Button with text “Delete” which is also hidden
  • note to show note contents

Contents in bold are basically made hidden, that’s why these are not visible on device, and we don’t even sure about the properties bind with all of them.

On hierarchy view widow each block represent a content, on tapping any content will load its position view, layout view and all parameters with their values.



TitleEditor Screen
TitleEditor screen shows following contents on device

It gives following idea about contents

  • textview to show “Note title” text
  • editview to show “Weekly List” text
  • button with text “Ok”

hierarchy viewer will show following contents hierarchy of above screen

It gives following idea about contents

  • textview to show “Note title” text
  • editview to show “Weekly List”
  • button with text “Ok”

On hierarchy view widow each block represent a content, on tapping any content will load its position view, layout view and all parameters with their values.


From above screens we can understand contents which are visible & hidden as we don’t have much idea about their Name, Id and other properties. Thanks to Robotium which provides us the way of calling contents by their indexes like in above screen, to enter some value in first EditFiled, we can write following line of code
solo.enterText(0, “10”);
Where the 0 represents the index or number of content on screen, as it is first EditField on screen, same we have to do with other controls.

This is fine so far and also works fine in simple cases, but suppose if there is some hidden content used within the app then how will we decide what is its index, and we will face issues in case. Hierarchy Viewer tool has covered this gap and know we can get detailed info about all parameters associated with any content.

Dealing with common Robotium Issues

Common Robotium Issues
When working with Robotium one may encounter issues that are due to the SDK, Eclipse or how Robotium works.  

The intent of this tutorial is to explain and show solutions to  common issues that new users experience.

1. NullPointerException when creating a test project for the first time

This problem comes especially with novice, when they for the first time create a new workspace and start working with Robotium.

If we create a new workspace for this tutorial, when creating the Test Project, Eclipse will show error ‘java.lang.NullPointerException’, because it is a new workspace and eclipse is not able to get selected Android resources for first test project.


We can solve this issue by following any of the suggestions below:

1. Create another TestProject & eclipse will automatically get selected resources(for the new one) & not show error for second created Test Project(we can use the second one for our work)

OR

2. First create an Android Project (we can also use Android Sample application bundled with Android SDK), run it, then create Test Project will not show error

2.Java.lan.NoClassDefFoundError: com.jayway.android.robotium.solo.solo

In the latest Android SDK versions (17 or above)  java.lang.NoClassDefFoundError:
com.jayway.android.robotium.solo.Solo error is shown if the Robotium jar is not exported. To fix the issue, after adding the Robotium jar go to the “Order & Export” tab and click the check-box besides the Robotium Jar and then click “OK”. Please see the screenshots below.



3. Robotium does not work with all the activities

Important to know that Robotium runs in the same process as the application under test. Due to this Robotium only works with the activities and views within the defined package (see below).  


In the AndroidManifest.xml we use the following code to describe which application to test:


android:targetPackage=”com.app.testingapp”
android:name=”android.test.InstrumentationTestRunner” />

“com.app.testingapp” is the package name of the application under test. Robotium is locked to this package.

4. Unstable test case

Its important to continuously use the waitFor methods, especially if new windows open or loading screens are shown. The waitFor methods tell Robotium to wait for a condition to happen before the execution continues.

Robotium contains the following wait methods:

waitForActivity(String name)
         Waits for the given Activity.

waitForActivity(String name, int timeout)
         Waits for the given Activity.

waitForDialogToClose(long timeout)
         Waits for a Dialog to close.

waitForFragmentById(int id)
         Waits for a Fragment with a given id to appear.

waitForFragmentById(int id, int timeout)
         Waits for a Fragment with a given id to appear.

waitForFragmentByTag(String tag)
         Waits for a Fragment with a given tag to appear.

waitForFragmentByTag(String tag, int timeout)
         Waits for a Fragment with a given tag to appear.

waitForLogMessage(String logMessage)
         Waits for a log message to appear.

waitForLogMessage(String logMessage, int timeout)
         Waits for a log message to appear.
waitForText(String text)
         Waits for a text to be shown.

waitForText(String text, int minimumNumberOfMatches, long timeout)
         Waits for a text to be shown.

waitForText(String text, int minimumNumberOfMatches, long timeout, boolean scroll)
         Waits for a text to be shown.

waitForText(String text, int minimumNumberOfMatches, long timeout, boolean scroll, boolean onlyVisible)
         Waits for a text to be shown.

waitForView(Class viewClass)
         Waits for a View of a certain class to be shown.

waitForView(Class viewClass, int minimumNumberOfMatches, int timeout)
         Waits for a View of a certain class to be shown.

waitForView(Class viewClass, int minimumNumberOfMatches, int timeout, boolean scroll)
         Waits for a View of a certain class to be shown.

waitForView(android.view.View view)
         Waits for a given View to be shown.

waitForView(android.view.View view, int timeout, boolean scroll)
         Waits for a given View to be shown.

5. Hanging/Freezing test cases

During test execution we open multiple activities, which stay alive depending upon the free memory available.  Therefore use  solo.finishOpenedActivities() in your tearDown method, it will close all the opened activities and free resources for the following test executions.

@AfterClass
   public void tearDown() throws Exception    
   {    
       solo.finishOpenedActivities();
   }

6. Problems when resigning APK

Use resign tool to re-sign an APK, observe that tool doesn’t work with Java 7(Java 1.7).

Manually re-sign APK
To manually re-sing APK:s follow the below steps:

* Un-zip the APK file
* Delete the META-¬INF folder
* Re‐zip the APK file

       It will zip file as “applicationName.apk.zip” , rename it to: “applicationName.apk”
* In Dos prompt/Terminal write following commands
> jarsigner -keystore ~/.android/debug.keystore -storepass android -keypass android applicationName.apk androiddebugkey
> zipalign 4 applicationName.apk TempApplicationName.apk
Then rename TempApplicationName.apk to applicationName.apk  if you want.

Test Android Apk with Robotium Using Eclipse Indigo – Latest

Test Android apk file with Robotium
To test an Android apk file using Robotium, it requires to

  • Install apk file on device we will use AndroidCalculator apk file, click here to find apk
  • Create Test Project using eclipse to test that apk


Note: If android working environment is not set on your machine, follow Set up Android Working Environment

1. Install AUT (Application Under Test) APK
Apk file can be installed on device or emulator. Before installing it needs to make sure that apk file is signed in debug mode. If not it can be signed as debug mode in next steps.
If apk file already has debug mode signatures skip next steps and jump to Load Emulator section.
Note: if you are not sure about the current signed mode of apk file, it’s better to resign it.

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. We will un-sign already signed apk file and then again sign it with debug key. 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:
1.If you know the certificate signature then use the same signature in test project
2. If you do not know the certificate signature then delete the certificate signature and use the same android debug key signature in both the application and the test project
3. If the application is unsigned then sign the application apk with the android debug key

To un-signed a signed application download java program from http://www.troido.de/re-sign.jar double click on it to open it and drop apk file on it
Or
* Un-zip the apk file
* Delete the META-¬INF folder
* Re‐zip the apk file

It will zip file as “applicationName.apk.zip” , rename it and make it as “applicationName.apk”
* In Dos prompt/Terminal write following commands

> jarsigner -keystore ~/.android/debug.keystore -storepass android -keypass android
applicationName.apk androiddebugkey
> zipalign 4
applicationName.apk TempApplicationName.apk

Then rename TempApplicationName.apk to applicationName.apk if you need.

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) created in Set up Android Working Environment, it will load emulator in few seconds.
If it shows some error most probably the Android SDK path in not set in PATH environment variable. One can set up Android SDK path in next step, skip next step if 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/tools/
export PATH=${PATH}:/home/naveed/android-sdk/platform-tools/

now reload the Terminal and run emulator -avd 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

set PATH=${PATH}:c:/naveed/android-sdk/
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

Install AUT apk on Emulator

After the emulator is working, we can now install AUT (application under Test) apk on emulator. To install apk 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 showing the directory where AndroidCalculator.apk is located, in my 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

Note: For sake of understanding Test Project app will be installed on emulator which will then call/load the AUT (already installed) and perform test scenarios on it. When we will run our Test project through Eclipse it will automatically install it on emulator (will do it in coming sections)

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




From ‘New Android Project’ Window, enter Test Project Name as ‘TestProject’, and click on ‘Next’



On next window under ‘Select TestTarget’ section select ‘This project’ option & click ‘Next’



On ‘Next’ window under ‘Select Build Target’ section select your desired SDK version, we can select Android 2.3.3 or latest of your choice & press Finish.

Eclipse will create TestProject with no package,



To manually create package right click on project select New > Package. On ‘New Java Package’ window fill package name against ‘Name:’. Like to test AndroidCalculator which has com.calculator package name, TestProject should be com.testcalculator or com.calculator.test package name


3. Create Test Class
Right click on com.testcalculator package, from New click on Class to add class. Enter class name TestApk (one can put and any name suits) and click on Finish



4. Code Test Class
Enter following code into new created TestApk class 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 Class launcherActivityClass;
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 integer/decimal value for first editfield, we are writing 10
solo.enterText(0, “10”);

//Enter any interger/decimal value for first editfield, we are writing 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 {
solo.finishOpenedActivities();
}

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

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



Put ‘com.calculator’ as value of following parameter like above
< span=””>“com.calculator”

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 automatically load AndroidCalculator.apk (installed on emulator) and
* 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


Test can also be run using command prompt/terminal by following 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 /AndroidCalculatorTestApk.apk

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

Note: Click here to download TestProject project source code

Playing with Android Application controls through Robotium Part 1

Android application is combination of multiple controls respective to functionality like to show text on screen developers use TextView, to show input field they use EditText, to show button they use Button and many other controls as

1. TextView To show text

2. EditText To show input field

3. Button To show button

4. ImageView To show multimedia image

5. ImageButton To show Image button

Each control has its own class comprise of multiple functions to work intelligently. Robotium is automated testing tool for Android applications. It identifies the control used on screen and allow user to interact with control through different ways. Robotium api provides the solo class which contains long list of functions to simulate real user actions on controls like Click, LongClick, Drag, Scroll up, Scroll Down, Click on ToggleButton etc..

To automate Android application test engineer can use both

1. Control’s native class

2. Robotium functions list for that specific controls

Like to work with TextView test engineer can use the TextView class and Robotium functions for TextView like getText, getCurrentTextViews(parent) and many others. Combination of both can make almost everything possible to automate android applications.

For new test engineers it’s quite tough to work intelligently with controls used on screen unless they know controls in brief. In this discussion/tutorial we will take each control one by one, explain its use and try to find out the combination of tricks we can use to work intelligently with these controls.

1. TextView

TextView is used to show text on screen, there can be any number of TextView controls on a screen depends upon the need of UI like a list showing results in rows may contains single TextView on each row or multiple on each row. We will go through each possible scenario and try to work with it

If there are 2 TextView controls on a screen to get their values we can use following Robotium function

String vTextViewValue = solo.getText(index).toString();

Index shows the number of TextView control used on screen, for first TextView it will be 0,

String vTextViewValue1 = solo.getText(0).toString();

and for second text view it will be 1 like.

String vTextViewValue2 = solo.getText(1).toString();

If there is a TextView control with some text we can get it using

TextView vTextView = solo.getText(“text”);

getText(“text”) will search and return the TextView control on screen matching the “text”. vTextView can use all functions of TextView class like to getId, getScaleX, getScaleY, getCurrentTextColor etc.

If there are many TextView controls in a list on a screen we will get the list on screen and then all TextView controls in that list,

ListView lvResults = solo.getCurrentListViews().get(0);

ArrayList vTextViewResults = solo.getCurrentTextViews(lvResults);

First line is getting the first list used on screen, and second line it getting all TextView controls in ArrayList. All TextView controls in specific list are stored in ArrayList, one can easily work with them.

2. EditText

EditText is used to enter text on screen, there can be any number of EditText controls on a screen depends upon the need of UI. Using Robotium we can write in EditTex and also get already written value. We will go through each possible scenario and try to work with it

If we need to enter some value in EditText on screen, we can write by using

solo.enterText(index, “value”);

the index parameter is used to call the number of Edit/Text on screen, like if there are two EditText controls on a login screen 0 is for first one and 1 is for second one on login screen,

solo.enterText(0, “username”);

solo.enterText(1, “password”);

If we want to get already written value in some EditText we can

String vEditTextValue = solo.getText(0).getText().toString();

the 0 represent the first EditText on screen, and value is saved in vEditTextValue variable. We can get the value of any EditText on screen by entering its index.

If there are many EditText controls on a screen we will get all EditText controls like,

ArrayList vEditTextesults = solo.getCurrentTextViews();

Above line will get all EditText controls on screen and save them in ArrayList, one can easily work with them.

We can get EditText like

EditText vEditText = solo.getEditText(index);

index is number of control on screen, it will return the EditText object and all functions of EditText Class of Android SDK are available to use.

We can also get EditText like

EditText vEditText = solo.getEditText(string);

string is value of control on screen, it will return the EditText object and all functions of EditText Class of Android SDK are available to use.

3. Button

Button is used on screen to click on some action, through Robotium user can click on button by,

solo.clickOnButton(index);

index shows the number of buttons on screen, if user needs to click on first button there will be 0 in place of index & 1 in case of second button on screen.

User can also click on button by identifying text written on screen like

solo.clickOnButton(string);

String shows the text visible on button, like if user needs to click on ‘Submit’ button there will be

will be

solo.clickOnButton(“Submit”);

User can also get all buttons on screen if there are many like

ArrayList vButtons = solo.getCurrentButtons();

Above line will get all Button controls on screen and save them in ArrayList, one can easily work with them.

We can get button like

Button vButton = solo.getButton(0);

0 represents the first button on screen; there will be 1 if user needs to interact with second button on screen. It will return the Button object and all functions of Button Class of Android SDK are available to use.

We can also get button like

Button vButton = solo.getButton(string);

string represents the text on button, there will be “Submit” if user needs to interact with Submit button on screen. it will return the Button object and all functions of Button Class of Android SDK are available to use.

4. ImageView

Images are used to show graphics on screen, Through Robotium user can interact with images easily

User can click on image like that, index shows the number of image on screen, user will enter 0 if there needs to click on first image on screen, and 1 if it needs to click on second image on screen

solo.clickOnImage(index)

If there are many ImageView controls on screen, user can get all of the them through Robotium like,

ArrayList vImageView = solo.getCurrentImageViews();

Above line will get all ImageView controls on screen and save them in ArrayList, one can easily work with them.

We can get button like

ImageView vImageView = solo.getImage(index);

0 represents the first image on screen; there will be 1 if user needs to interact with second button on screen. it will return the ImageView object and all functions of ImageView Class of Android SDK are available to use.

5. ImageButton

There can be button images used in Andorid Applications, Through Robotium user can easily interact with them.

User can click on imageButton like that,

solo.clickOnImageButton(index);

index shows the number of imageButton on screen, user will enter 0 if there needs to click on first imageButton on screen, and 1 if it needs to click on second imageButton on screen

If there are many ImageButton controls on screen, user can get all of the them through Robotium like,

ArrayList vImageButton = solo.getCurrentImageButtons();

Above line will get all ImageButton controls on screen and save them in ArrayList, one can easily work with them.

We can get button like

ImageButton vImageButton = solo.getImageButton(index);

0 represents the first ImageButton on screen; there will be 1 if user needs to interact with second button on screen. it will return the ImageButton object and all functions of ImageButton Class of Android SDK are available to use.

Note: Soon will upload the next parts with more controls

Design Data Driven Framework around Robotium

In my last tutorial we tested Test Android apk file with Robotium. Simple Android Calculator’s apk was used with some specific input values. Now in this tutorial we are extending testing to Data Driven Testing approach under Robotium. Many automate testers may have worked on Data Driven Testing Framework for web sites or Desktop applications.Link
There are many Data Driven Frameworks available, but to save time I have tried to develop me own data driven framework. It may not mature enough but we can make it more intelligent by extending it.

1. Data Driven Testing Architecture

I am going to define my test scenarios into a csv (comma separated version) file and my script file will

* Read input from TestData.csv file
* Enter each ‘First Value’ column data in first edit field
* Enter each ‘Second Value’ column data in second edit field
* Click on Multiply field each time
* Verify Resultant value

To create a data driven development we will go through following steps

* Create Test Data
* Push Test Data into Emulator
* Import jar file
* Create Test Script (read data and assign values to AUT(application under test)
* Execute Test scenarios
* View Test Results

We will apply Data Driven testing approach on Android Simple Calculator. In which we enter two values and on clicking Multiply button it will show their resultant value. We will use Test Android apk file with Robotium project for Data Driven Testing implementation.

2. Create Test Data

We can create any combination of First and Second Value to test our Simple Calculator on Android. First Value represents the values for First Editfield and Second Value represents the Second Editfield in Android Simple Calculator. Execute column will allow the script to execute specific scenarios only.

3. Push Test Data into Emulator

Robotium will run our test script into emulator, for proper implementation of Data Driven Testing we need to make Test Data file accessible for emulator.

Load emulator, from eclipse Load DDMS interface. From Project Explorer tab navigate to the AUT package (our Android Simple Calculators package is com.calculator) and expand it to files category. It will be empty, we will push our Test Data file into files directory and then our script will be able to read TestData contents with emulator.

From top right section click on push icon and browse the TestData file from your computer.

It will show the pushed test data file into files directory like below

4. Import JXL jar file

Our TestData file is in CSV format so we need to import necessary jar file to read contents, there may be multiple API available on Internet, I will be using JXL, it is available on following link.

After successfully download import that jxl.jar file into project build path.

5. Create Test Script

From Test Android apk file with Robotium project open the TestApk.java file and replace the existing code with one (same file name) in shared project code.

6. Run Test Script

Now its time to run our test script, From eclipse right click on project, select Run As and then click on Android JUnit Test. Wait for some time it will show the progress in console bar like below


System will load Android Simple Calculator app into emulator and start to read TestData file and enter First and Second Values into calculator and asserts resultant values. I have put (System.out.println) code to print results, you need to load LogCat interface and results will be visible there in logCat window like below,

After completing execution eclipse will show successful script execution like below.

7. View Results

After successful execution test script will create and write results into txt file and that file will be visible into Project Explorer of DDMS.


Click on pull icon top right to put that file and view it. Results of all test scenarios will be visible into TestResultData file.

Note: This is the basic Data Driven Testing framework around Robotium. There are many things which can be implemented in better way. I have tried to develop it in simplest way. Its up to the test engineer to use it in his/her own way. We can make it as much intelligent as we need.

Download sample project code with TestData file

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.