Friday, July 1, 2011

Black Box testing for Android Using Robotium.



Requirements:

  1. Android application APK file for testing.
  2. Eclipse for building the Test Project.
  3. ADT(Android Development Tools)
  4. SDK(Software Development Tools)
  5. JDK(Java Development Kit)
  6. Robotium-solo-X.jar


Prerequisites for creating test project:

  1. Install Eclipse ADT,SDK,JDK to your system.
  2. After Installation give proper path in the environmental variable.


Step 1 : CREATE TEST PROJECT IN ECLIPSE

Create the test project by :

File_New_Project_Android_Android Test Project






Click on Finish by selecting the Build Target.

Step 2: Do the Following changes in “AndroidManifest.xml”

Open the package “SampleApplicationTesting11” and go the Manifestfile.




Exact package name can be know by logcat from the command prompt.

Launch the emulator.

Add install testapplication.apk

adb logcat

Run the application once you will get to know the exact package name.

Step 3: Create a Class file under your package as shown below .




Click on the class and create a java file and copy the below code .

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

public class SampleExample extends ActivityInstrumentationTestCase2
{
private static final String TARGET_PACKAGE_ID ="com.yellowbook.android2";
private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME ="com.yellowbook.android2.AndroidSearch";
private static Class<?> launcherActivityClass;

static{
try
{
launcherActivityClass = Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
}
catch(ClassNotFoundException e)
{
throw new RuntimeException(e);
}
}
@SuppressWarnings("unchecked")
public SampleExample() throws ClassNotFoundException
{
super(TARGET_PACKAGE_ID, launcherActivityClass);
}
private Solo solo;
@Override
protected void setUp() throws Exception
{
solo = new Solo(getInstrumentation(),getActivity());
}
public void testCanOpenSettings(){
solo.enterText(0, "Pizza");
solo.enterText(1, "nyc");
solo.clickOnButton("Search");
//solo.clickOnText("Search");
solo.waitForText("Best Match");
assertTrue(solo.searchText("Best Match"));
}
@Override
public void tearDown() throws Exception
{
try
{
solo.finalize();
}
catch
(Throwable e)
{
e.printStackTrace();
}
getActivity().finish();
super.tearDown();}}


Note:

private static final String TARGET_PACKAGE_ID ="com.yellowbook.android2";
private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME ="com.yellowbook.android2.AndroidSearch";

“com.yellowbook.android2” is the package name.

com.yellowbook.android2.AndroidSearch is the activity name .


Now import the required jar files from the build path.

Step 4: The Apk file has to have the same signature as your test project.


The important points to understand about signing Android applications are:
  • All applications must be signed. The system will not install an application that is not signed.

  • You can use self-signed certificates to sign your applications. No certificate authority is needed.

  • When you are ready to release your application for end-users, you must sign it with a suitable private key. You can not publish an application that is signed with the debug key generated by the SDK tools.

  • The system tests a signer certificate's expiration date only at install time. If an application's signer certificate expires after the application is installed, the application will continue to function normally.

  • You can use standard tools — Keytool and Jarsigner — to generate keys and sign your application .apk files.

  • Once you have signed the application, use the zipalign tool to optimize the final APK package.

The Android system will not install or run an application that is not signed appropriately. This applies wherever the Android system is run, whether on an actual device or on the emulator. For this reason, you must set up signing for your application before you will be able to run or debug it on an emulator or device.

We can use re-sign.jar to get the debug apk file .

Once the Apk files is signed , install the apk into your emulator or device and run the java program as Android Junit Test.



2 comments:

  1. hi,

    If I am having apk and does not have the source code...Do i need to create Android Project and then i need to create Test project. If i directly creating the Android Test Project i am getting Null Pointer exception. So If i need to first create Android Project, that project should contain what?


    I am working on this almost 3 weeks.. I did not get a clear picture on this. No body has written a post from the scratch. Can you please answer me the above question

    ReplyDelete
  2. I went onto your blog while centering just somewhat submits. Pleasant technique for next, I will be bookmarking without a moment's delay grab your entire ascents
    penetration testing

    ReplyDelete