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
the
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
* Write following command to install AndroidCalculatorTest apk on emulator
> adb install
* Run the test cases:
> adb shell am instrument ‐w com.testcalculator/android.test.InstrumentationTestRunner
AWESOME.. very detailed. But I did not get one explanation – > zipalign 4 ApplicationToTest.apk TempApplicationToTest.apk
Then rename TempApplicationToTest.apk to ApplicationToTest.apk
Why are we renaming TempApplicationToTest to ApplicationToTest?
Nice Post. I followed these steps, but unfortunately i get a NoCallsDef found for Solo class, although i have added the robotium jar(2.3) in my build path and selected it as well in the export dependencies in eclipse.
Looks like the jar is not packaged in the test apk, any idea how can i solve this ?
Yap it seems like that, can you please share your project code to better understand the problematic area ?
nice post .. but i am tatally confused on the new test project window ..
what i will write in the following two field in that form ??
an existing andriood project :
we are working in apk so what can we write here?? but without a form i can finish the process???
This comment has been removed by the author.
Niloy,
Sorry for the late reply.
Due to short time lines, I could not spend time to look into it, I also got that problem, you can do what i did that time 🙂
1. Select option 'Add Existing Android Project'
2. Click on 'Browse' and select project, you can make a TestProject with the package name of your 'target project', same as i had 'com.calculator'
3. On selecting 'TestProject' it will automatically load its package against 'Test Target Package'
4. Now remove name of your 'TestProject' name against 'Add Existing Android Project'
5. Fill the rest of form, it will work that way
Note: It may not the right way, but it worked for me :), will surely share something proper on it.
I have problem with this… I have mp3 for sound effect. when test it, I have null pointer that mp3 file is missing. Any solution?
Hi,
First of all sorry for late reply. Can you please give me more details about your project? or you can share if its possible.
Hi,
I'm facing problem…
In New Android test project window i'm not getting the properties(Application name,package name MIN SDK version). Before crating Test project am i need to crate any project?
Thanks
Hi,
If you are creating a new testproject, you should enter these values manually.
1. You can get application package name from logcat
2. In most of the cases we are not sure about exact MinSDKversion value of apk, so lets play hit & try rule, and put your latest installed SDK value as MinSDKVersion value.
Note: there may be some way out to find out MinSDKVerion of apk, I will try to find out and update
Thanks for reply.
Now able to find the package name from logcat.But my problem was, in New Android Test Project window there is no such fields(Application name,package name MIN SDK version)I'm using Eclipse-jee-indigo.
If you are creating testProject with androiProject it will not show that options, like create a AndroidProject in eclipse and next window after entering project name will be 'create test project' window for that AndroidProject, it will not show versions, minsdk etc.. options
If you will create independent testProject it will show these options, like in above example.
I'm using Eclipse-jee-indigo. Is this correct or i have to use something else. suggest Eclipse version.
That's fine, otherwise you can get latest one available at their url
I used apk signer to resign my apk. Then when trying to install it on my device(through usb debugging) with "adb install test.apk" I get a failure: [INSTALL_PARSE_FAILED_NO_CERTIFICATES]. Any idea where it might be the problem? adb install works ok for the release apk, signed with a custom signature.
How to identify whether my application is signed or not?
You will get 'intrumentation… signature not matching….' error when we try to run TestProject on unsigned app 🙂
Hey
It very good tutorial to run scripts on emulator…… is their any way to run entire test cases on real android device…..if so any one plz tell me how to execute from scratch level
thanks in advance
Hey
It very good tutorial to run scripts on emulator…… is their any way to run entire test cases on real android device…..if so any one plz tell me how to execute from scratch level
thanks in advance