Thursday, December 27, 2012

Getting Started with Robotium

Just before getting started, let us discuss few things about Robotium.
Robotium can be used to test applications where source code is available and applications where only APK file is available. 

In this example, let us discuss about the case when source code is available. I will discuss the other scenario in a separate post. let us start with setting up test environment first,

Requirements :

  • Eclipse for building the Test project
  • ADT (Android Development Kit)
  • Android SDK (Software Development Kit)
  • JDK (Java Development Kit)
  • Latest Robotium Jar file


Prerequisites for Creating Test project :

  1. Install JDK, Eclipse, SDK and ADT to your system
  2. After installation, set proper path to Environment variables JAVA_HOME and ANDROID_HOME and add the SDK and JDK installation path to environment variable PATH.
  3. Download the Latest Robotium jar file from http://code.google.com/p/robotium/downloads/list 

Create a Test project in Eclipse:

  • Create the Test project by "File --> New --> Project --> Android --> Android Test Project", the window will open
    • Enter the Project Name, click on 'Next'
    • if project source code is already available/imported in eclipse, in the "Test Target" field, choose 'An existing Android project' and select from the list of Projects, click on 'Next'
    • in the "Build Target", select the application development platform, suppose,  if application is developed on Android 2.2, choose API level as 8 and so on..
    • click on 'Finish', a new test project will be created in the Eclipse.

Add the Robotium jar file to the Project

Right click on your project, --> Build Path --> Configure Build Path --> select "Libraries" tab
Then select "Add External Jars" -> select the Robotium Jar file downloaded
go to "Order and Export" tab, select the check box next to the Robotium jar file.

Create a Test case :

  • Create New class file:
    • select Project folder  --> "src" directory --> select package, Right click on it, select "New --> Class", enter the class name and click on 'Finish'.
    • extend your class to ActivityInstrumentationTestCase2
    • when eclipse shows error, click on import statement, which will import 'android.test.ActivityInstrumentationTestCase2'
    • copy the following code to your project and do modifications when required
   1: public class <your class> extends ActivityInstrumentationTestCase2{  //no need to copy this line    
   2:     private Solo solo;
   3:     // replace the "SampleTest()" with your class name ex: MyTest()
   4:     // replace "MainActivity.class" with your application project's starting activity class name 
   5:      public SampleTest() {           
   6:          super(MainActivity.class);
   7:       }
   8:     //setUp() is run before a test case is started. This is where the solo object is created.     
   9:    @Override
  10:     public void setUp() throws Exception {
  11:         solo = new Solo(getInstrumentation(), getActivity());
  12:     }
  13:    //tearDown() is run after a test case has finished. 
  14:    //finishOpenedActivities() will finish all the activities that have been opened during the test      execution.
  15:    @Override
  16:    public void tearDown() throws Exception {
  17:       solo.finishOpenedActivities();
  18:    }
  19:   // your test case starts here... 
  20:   // Test case name starts with keyword "test"followed by your Test case name in Capital Letter
  21:   //here, "MyFirstTestcase" is the test case name, change this with your desired name . 
  22:   public void testMyFirstTestcase() throws Exception {
  23:              // write your test code
  24:   } 
  25: } //no need to copy this line

Now you can write your Test case in the test block

Next : How to Run the Test? -- see the Post 

1 comment:

  1. I am currently working on robotium. I have the apk of an hybrid application.

    Is the any way i can identify the attributes of the web element(like name , css , id etc). Pls let me know if there is any way to do it...

    ReplyDelete

Note: Only a member of this blog may post a comment.