Showing posts with label Android. Show all posts
Showing posts with label Android. Show all posts

2013-06-25

Tips-Tricks in Robbotium(Android Automation Framework)

In this article we are going to see some tecniques that can be applied in robotium to use it smartly. This is not a complete post, I will add more tips & tricks as the time goes. For basic ideas on robotium, please see my Mobile Testing section. Details , step by steps posts are there.

Sample test template :

I have created two projects for same test case where both are two ways to test same app(one with code, and another is without code). The concept is, there will be a base class maintaining all communication with android test runner. And all test classes will extend that class . All test classes will only contain test methods where as the base class will contain the main android unit test extension , setup, tear down. Here is a screenshot for more details.
Project with source code
Project without source code

How to run Robotium Test Case?
-Run Emulator or device
-Check that the device is connected using "LogCat" /"Device from eclipse or using adb commands in command prompt.  In "LogCat" or"Device", we can see device responses.
-Install debug version of app if you are using apk file to test(no source codes)
-Right click the the package you want to run in side eclipse(or the whole project)
-Select run as Android JUnit Test. If it is not in here, select Run Configuration , and use android junit test.
-Now , we see Emulator activity from log cat and test will be executed. In eclipse, we will see junit runner describing the test progress & results.

Debugging from Emulator and Eclipse : 
- Use "LogCat" and "Device" in eclipse to see app's activity. Spatially what it is doing or interacting. screenshot.
To add "LogCat" or "Device, from eclipse, go to window > show view . You will find device and logcat. If not , click other and you will see full list. Screenshot.
- Try to avoid filter in log cat and use verbose type logcat view so that you can see detail activity of emulator . Spatially test application's activity and responses from android system . If you need to monitor specific activity , then use filter.
- From device, we can do a lot of advance and interesting stuffs like
a. Method profiling : We can see a method's resource using , run time and all other information.
b. Dump View Hierarchy : We can dump view Hierarchy for automated UI tests.
c. Take screenshot : We can take screen shot any time we want. It will help for identifying bugs.
d. Capture system wide trace :


Thanks...to be continued...:) 

2013-06-19

Android APK(No Source Code) Test in Robotium

In this article we are going to see how can we write unit tests with Robotium Framework when we do not have the source code. That means we are going to test an application using its installer apk file.

Please read my previous post for robotium setup basic


In this post we will only see the test Class used for testing stand alone APK (not with code). So, please see my this post for code start basic. I will mainly focus on test class here.

To test a stand alone application, we need to have debug version of APK file. To convert an apk to debug version, see my this post.

Like as following this post, we will not add dependency project. In stead of this step, we have to install the application in the emulator.
And add android test project in eclipse with robotium library. Now add a class(APKTestCase).

So, after adding a test class( my example class name is APKTestCase ), we will extend from ActivityInstrumentationTestCase2 without parameter(as we don't have codes, so we do not know)
So, my test class will be.

public class APKTestCase extends ActivityInstrumentationTestCase2{
}And the constructor 
public APKTestCase() throws ClassNotFoundException{
        super();
}



This constructor needs class name as parameter. So, how to get the class name ? We have to be very careful here. While testing with robotium, only activity name is not enough , we have to specify the class name. The screen shot is showing how to define the class name with pacakage name. This is called full class name.
So, if we do not have source code, so how can we get the class name. This is a bit tricky.
-Run eclipse and open an emulator.
-Run your installed application(if not installed, then install it first using ADB commands, use my this post)
-From eclipse, see log cat messages in no filtered mode.(usually in verbose)
After starting activity, system process shows in text section.
you can see the message like this <activity neme>/.<Class Name> : +<time> . See the screen shot
Here is the screen shot

So our full class name will be. com.example.android.notepad.NotesList
Now, we have to convert this class name into class by an identifier. To do this we will use a spatial class named as Class to identify. We will use it as static as it is accessing static class name. So for this part , our code will be
private static final String Launcher_Activity_ClassName  = "com.example.android.notepad.NotesList";   
private static Class launcherActivityClass;
static{
        try{
            launcherActivityClass = Class.forName(Launcher_Activity_ClassName);
        }catch (ClassNotFoundException ex) {
            throw new RuntimeException(ex);
        }        

}
And the constructor will become
public APKTestCase() throws ClassNotFoundException
    {
        super(launcherActivityClass);

}

Like as standard test case, we need to override setup & tear down. So, finally class will be

@SuppressWarnings("unchecked")
public class APKTestCase extends ActivityInstrumentationTestCase2{  
    private Solo mySolo;
    private static final String Launcher_Activity_ClassName  = "com.example.android.notepad.NotesList";  
    private static Class launcherActivityClass;
    static{
        try{
            launcherActivityClass = Class.forName(Launcher_Activity_ClassName);
        }catch (ClassNotFoundException ex) {
            throw new RuntimeException(ex);
        }      
    }

    public APKTestCase() throws ClassNotFoundException
    {
        super(launcherActivityClass);
    }   
    @Override
    public void setUp() throws Exception
    {
        mySolo = new Solo(getInstrumentation(), getActivity());
    }
    @Override
    public void tearDown() throws Exception
    {
        mySolo.finishOpenedActivities();

}

Now we are ready for writing test case. We can add any sample test case. You may see a sample project link in here.

Thanks...:)

2013-06-17

How to write test case in Robotium with source code

In this article we are going to see how can we write unit test case for a android application. We will discuses about a small demo example with detail explanation. The objective of the post is to have clear idea on how to get started with robotium uni test case writing for a invoice.

First we need a project which we will test.We can download a notepad project which is used for learning purpose with robotium from here. We will get two projects there, one project containing application code base another one is robotium test case. Here is the link of the same project from my drive. We will use the code base.We will add the code base to eclipse. To do that, just import the downloaded code base. In eclipse, From, File >Import > Select Archive file under General > show the downloaded ZIP file and finish the process.

To setup robotium environment, see my this post. So, we will start from adding a android test project.[ As all previous steps are shown in my post]

After adding a test project, we get a package. We can edit the package names from android manifest in case we want to change the package name(for test project). Here is the screen shot.

Now, we have to add target package in current application manifest. To do that,
-Click AndroidManifest.xml
-Click Instruments tab
-Click the android.test.InstrumentationTestRunner
-From Target Package, provide the target package name.
(from this example : com.example.android.notepad)
-Screenshot
Now save this. We can do the same thing by adding this
 <instrumentation android:targetPackage="com.example.android.notepad" />
directly to AndroidManifest.xml
Screenshot

So,after renaming I have made my project as com.notepad.test this package and target package is com.example.android.notepad. Screen shot

Now, we need to include the main project(which we will test) as dependent of our test project. So that, when we will run the test case, the application apk will be installed in the test environment.  To do that.
-Right click on test project and select properties
-Click Java build path
-Select project tab and click add
-Select NotePad project and press ok
Screenshot

So Now, Add a class under a package of src folder .
After a class creation , extend the class from base class of " ActivityInstrumentationTestCase2 ".
So what is this "ActivityInstrumentationTestCase2" ?
This is a unit test case template(base class) that provides all controlling over a app activity for unit testing. For more detail you may see the declaration. We need to override setup and tear down functions. For those who are new at unit testing, please see junit or nunit unit test case basic. I have an article on nunit.

When extending , we will use our target activity class name(assuming that we have source code) as parameter. In here, we have to be careful as wrong class name will cause error while execution. That means, we have to choose the functions to test. From this example, we are going to test adding/deleting/editing notes. So, from code we have check which class is active(actively responsible) for those functions. in here Notelist class is responsible for showing all notes and it creates or handle all types of note operating events. So, we need this class as parameter while extending "ActivityInstrumentationTestCase2". My testing class is TestNoteAdministration So the code will be
public class TestNoteAdministration extends ActivityInstrumentationTestCase2<NotesList>{
}

-And add the constructor.
public TestNoteAdministration() {
   super(NotesList.class);        

}
This is spatial constructor for test case. This actually calls super class's constructor with test class activity as parameter. When the test run, this activity get initiated.

-Add a Solo Class's object. This is our main robotium functional test operator object. We have to initialize in setup.
private Solo mySolo;

-Now we need to override Setup and Teardown Methods.

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

In here , getInstrumentation used for initializing settings from manifest and getActivity initiates the test activity.

 @Override
 public void tearDown() throws Exception {
        mySolo.finishOpenedActivities();

}
This will close all the operating activity.

Now we are ready to write a test case. Like as a Junit test method, we will write the methods. Example -
public void AddNoteTest() throws Exception {
//implement 
}

A sample test case with the test project will be found in this link. Like as importing note pad project, you may import and see the test code.

Note:
- We will see how not to use parameter in separate post. And, I will try to provide some sample code in separate post that I have worked with different projects.
- Before running, you may fix android properties. To do that , right click the project > android tools > Fix Project Properties.

...Thanks..:)

2013-06-16

Setting up Robotium in Eclipse

In this article, we are going to see how to install Robotium In Eclipse.We will use robotium for android application testing using code base or stand alone apk. I will provide separate post for how to code. Let's make environment ready.

Step 1. Install android SDK. See my this post.

Step 2. Download Robotium : Download latest robotium and java doc from this link . As we will perform detail analysis on robotium, you may download the source from this github link. I use zipped source.

Step 3 : Adding Robotium to the project : In this step, we will see how to create robotium project. Robotium is an extended Android JUnit package. So,
-Run Eclipse
-From file > New >Project
-Select Android Test Project [Under Android Folder, with Junit logo]
 Screenshot Link 
-Add a project name(ex-ShantonuTestRobo)
-Select This Project(not under any existing project)
-Select an android version( I choose 4.1.2) and click Finish. So we will find like this.

Now , we have to add downloaded library. For this, right click on project and select properties.
 -From property window, select Java Build Path.
-Click Libraries
-Click Add external JARs and show the downloaded path of robotium solo jar.
ScreenShot
-You may add Java Doc and downloaded source files. It will help advance unit testers.
Screenshot for JavaDoc
Screenshot for Source Adding.

Step 4 : Add a class under package of the source file . We will see two ways to write robotium unit test cases. I will post separately with codes.

Now we are ready environment to start coding with Robotium for Unit testing.

Note : To prevent java.lang.NoClassDefFoundError export the robotium library. To export any library go to project properties > java build path > order and export . Now select robotium and press ok. 

Thanks...:)

Setting up Android SDK in Eclipse

In this article we are going to see how to set up android SDK with eclipse.We are doing this for mainly two purposes.
1. When we try Robotium to for android unit testing, we need that
2. When we do code review or android junit unit test case writing
Beside that, for development we need to set up android sdk.

Step 1 : Download Eclipse 
We need to download Eclipse from this link.  I use eclipse classic, yo may use j2ee or java developer edition.

Step 2 : Download Java
We need to have JAVA(sdk) in the PC. I use x86 SDK, you may also try x64 SE editions. Download link. To check if java installed in you pc or not, you may check C:\Program Files\Java or C:\Program Files (x86)\Java folder having java SDK(for windows).
From command prompt if we use following command , we can also know , does the PC have Java or not.
java -version

Step 3 : Download Android SDK 
WE need to download android SDK from this link. 
For windows
For MAC
For Linux

Step 4 : Install SDK 
Install the SDK that we have just downloaded. 

Step 5 : Add to OS path (as system variable)
We need to add sdk path as environment variable. To add this in windows 7/8
Right click My computer > properties >advance system settings , then click Environment variable . Select Path > click Edit then add the path at the end with semi colon(;) . Now , press ok.
 
To check, it is working or not, open command prompt and write
adb devices
(please connect an android device to see the device ID). If you see, no command found, that means not done. If command is found , it will show device ID or blank (if no device)

Step 6 : Install Eclipse Android Plugins : We need to install android plugins in eclipse.
-To do that, open eclipse
-Click Help from Menu
-Click Install New Software
-Click Add button
-In name (name of the plug in), write ADT plugins
-In Location, write : https://dl-ssl.google.com/android/eclipse/
-Press ok, you will see the list after loading from Internet. Select developer tools and press next, next up to finishing process to install(with agree the agreements). Wait for the plugins to be downloaded. Then close eclipse and run again(Eclipse may prompt for restart it self, you can restart).
 

Step 7 : Locate android and update sdk : 
After installation, we will see this two icon in the tool bar as well as menu under Window.

When we click window > Preferences , we will see android.
Now, click window > Android SDK manager to get latest SDK tools
And, you may Click window > Android Virtual Device Manager to make your desired Virtual device.
 

So, now we are done with Android SDK setup. We may develop or test android codes.

Note : Now a days, android sdk can be downloaded as bundle(Android SDK+eclipse).

Thanks...:)