Friday, 10 March 2017

Working with Robotium - Robotium Quick Start

Robotium


Powerful Test Cases can be developed, without knowing source code of the application.
Robotium is an open-source test  framework  like Selenium but only for writing automatic black-box test cases only for Android applications.
Robotium can be used both for testing applications where the source code is available and applications where only the APK file is available and the implementation details are not known.
Robotium is similar to Selenium, but for Android. It has support for Android features such as activities, toasts, menus and context menus.
APK=Android application package file (APK) is the file format used to distribute and install application software and middleware onto Google's Android operating system.

What you are not able to do with Robotium :
Cross Activities testing, Robotium is able to work only with same certificate app, otherwise you will get inject events exception (eg :you are not able to do clicks on screen keyboard)
 Robotium has no mechanism to handle expected/unexpected alerts/popus/dialogues. For example iOs javascript tests has very simple boolean flag and callback for handling alerts
 Robotium has big problem with auto scrolling methods (possibly currently it is fixed) for example if you are looking for the text, which is not shown, Robotium will stack in the end of the scroll view and make assertTrue(false) to stop scrolling.
Robotium does not support flash and webbased applications.
Robotium does not work on Canvas.
BLACK BOX  TESTING  WITH  ROBOTIUM  ON  APK  FILES

Requirements:
1) Android application apk file for Testing.Ex: MetlifeTesting.apk
2) Eclipse for building Test project
3) ADT (Android Development Tools)
4) SDK (Software Development Kit)
5) JDK(Java Development Kit)
6) robotium-solo-3.1.jar

Prerequisites for creating test project:
*Install eclipse,ADT,SDK,JDK to your system.
*After installation give proper path in environmental variable

PATH: C:\Program Files\Java\jdk1.6.0_27\bin;D:\monkeyEclipse\android-sdk\platform-tools

 STEP 1: CREATE TEST  PROJECT  IN  ECLIPSE

Create the test project by:    
 FileàNewàProjectàAndroidàAndroid Test Project

Fill all fields like below format:
*Test Project Name: MetlifeTesting

*Test Target: Click on“This Project“

*BuildTarget:
If the application was developed using SDK Version 7 then select Android 2.1–update1.
 If it was developed by SDK Version 8 Then select Android 2.2.

*Properties : Applicationname : MetlifeTesting

*Package name: com.igate. MetlifeTesting

*Min SDK version:
Default value will be there according to Build Target selection Then click on “finish”.
  
STEP 2: DO THE FOLLOWING CHANGES IN “AndroidManifest.xml”

Change:
<instrumentation
android:targetPackage=" com.igate. MetlifeTesting "
To:
<instrumentation
android:targetPackage=" com.metlifeapps.metlifeus "

If you do not know The exact package name then type this in the DOS prompt
>launch the emulator
>adb logcat – cmd1
>adb install testapplication.apk – cmd2

 Run the application once and you will get the exact package name.

STEP 3: CREATE SAMPLE TEST CASE

Select The package and right click it and select:   New àClass


  
Copy this code into the editor:

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

public class HomePage extends ActivityInstrumentationTestCase2{

private static final String TARGET_PACKAGE_ID="com.metlifeapps.metlifeus";
private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME="com.metlifeapps.metlifeus.MetAndroidActivity";
               
                private static Class launcherActivityClass;
               
                static{
                                try          {
                                                launcherActivityClass=Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
                                                }catch(ClassNotFoundException e){
                                                throw new RuntimeException(e);
                                                }
                                }
                               
                @SuppressWarnings("unchecked")
                public HomePage () throws ClassNotFoundException
                {
                super(TARGET_PACKAGE_ID,launcherActivityClass);
                }
               
                private Solo solo; //Robotium class used to support test cases that span over multiple activities.
                @Override
                protected void setUp()throws Exception
                {
                solo=new Solo(getInstrumentation(),getActivity());//takes in the instrumentation and the start activity.
                }
// the test methods MUST begin with test...

                public void testMenuSelect(){
                                solo.pressMenuItem(0);
                }
               
                @Override
                public void tearDown() throws Exception {
                    solo.finishOpenedActivities();
                }
}



NOTE: Change TARGET_PACKAGE_ID  and  LAUNCHER_ACTIVITY_FULL_CLASSNAME ..

private static final String TARGET_PACKAGE_ID = " com.metlifeapps.metlifeus";
Private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME =
"com.metlifeapps.metlifeus.MetAndroidActivity ";

If you do not know The exact package name then type this in the DOS prompt
>launch the emulator
>adb install MetlifeAndroid.apk
>adb logcat

Or

>aapt dump badging appname.apk

Look for

package: name='com.igate.retail.ecoupon'
launchable-activity: name='com.igate.retail.ecoupon.activity.StartActivity'

Run the application once and you will get the exact package name.


STEP 4: ADD THE LATEST VERSION OF THE robotium jar FILE TO THE PROJECT.

Right  click on “MetlifeTesting” projectàBuild PathàConfigure Build Path


 Then   Select    Add External JarsàSelect  robotium.jar àfile à Open àOK





STEP 5:

The apk file has to have the same certificate signature that your test project has Signature:

The Signature will identify the author of the android application.Signature Means it contains the information like firstname 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.


NOTE:

* 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 shoulduse 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

PROCESS FOR GENERATING DEBUG KEYSTORE:

1)      Un-Zip the apk file.
2)      Delete The META--‐INF folder(Because META-INF folder contains certificates)
3)      Re-Zip the remaining files and rename as .apk.
(It will zip file as "MetlifeAndroid.apk.zip" , rename it to: " MetlifeAndroidNew.apk")
4)      In command prompt run below command:
             >keytool.exe -genkey -v -keystore debug.keystore -alias debug.keystore –keyalg
               RSA–validity  20000
               
(Or)
                This will definitely work:
C:\Users\bm812203\.android> keytool -genkeypair -alias androiddebugkey -keypass android
-keystore debug.keystore -storepass android -dname "CN=Android Debug,O=Android,C=US"
-validity 9999

keytool -list -alias androiddebugkey -keystore (path_to_debug_keystore).keystore -storepass
android -keypass android

jarsigner.exe -verbose -keystore debug.keystore  MetlifeAndroid.apk [give aliasname]

       5)   Create new folder in Eclipse workspace and put debug.keystore file .
     
    
 6)   Set custom debug.keystore path in eclipse:
             windows-->preferences-->android-->build





7)Right click on project àexportàdebug .keystore(path of sdk—platform tools)
STEP 6:  RUN THE EXAMPLE TEST CASES

Right Click on the test project     à Run As  àAndroid JUnit Test




STEP 7:  RUNNING TEST CASES ON THE EMULATOR/DEVICE WITHOUT USING ECLIPSE
1)      Put .apk file into android-sdk folder.

2)      Install the application apk:
>adb install MetlifeAndroid.apk

3)      Install the test project apk: (.apk copied from bin folder)
>adb install MetlifeTesting.apk
             
               4)   Run the test cases: (Here com.igate.MetlifeTesting is the package_name of your test    application)

   >adb shell am instrument -w com.igate.MetlifeTesting/android.test.InstrumentationTestRunner

For more reference go  to:





No comments:

Post a Comment