Skip to main content

Automate your Android App Testing Using Robotium



Creating a Simple Robotium Automated Test Case
  • First off, download the latest version of Robotium, (3.6 at the time of writing).
  • Now simply create a new Android application in the IDE of your choice, I prefer NetBeans due to familiarity, but I know most people like Eclipse for Android development.
  • Add the Robotium JAR as a reference in your project.
For the purpose of this example, let’s say we want to click the ‘Cancel’ button in the below screen capture. As I mentioned above, our Robotium code uses the string ID’s to find objects (based on the current device language, we will look for the value of that string ID for that language if available, and use that to look for the object on the current screen).
Code (Finally!)
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.jc.robotium
import android.app.Activity;
import android.test.ActivityInstrumentationTestCase2;
import com.jayway.android.robotium.solo.Solo;
import android.content.res.Resources;
import android.content.Context;
import java.util.Locale;
public class RobotiumExample extends ActivityInstrumentationTestCase2 // Provides functional testing of an activity
{
    private static final String TARGET_PACKAGE_ID = "com.yourcompany.yourapp";
    private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "com.yourcompany.yourapp.Activities.MainActivity";
    private static Class<!--?--> launcherActivityClass;
     
    private Solo solo;
    private Activity activity;
    private Resources res;
    private Context context;
    
    // This will launch the application specified above on the device
    static
    {
        try
        {
            launcherActivityClass = Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
        }
        catch(ClassNotFoundException e)
        {
            throw new RuntimeException(e);
        }
    }
     
    public RobotiumExample() throws ClassNotFoundException
    {
        super(TARGET_PACKAGE_ID, launcherActivityClass);
    }
     
    @Override
    protected void setUp() throws Exception
    {
        activity = getActivity();
        solo = new Solo(getInstrumentation(), activity);
        context = getInstrumentation().getContext();
        res = context.getResources();
    }
     
    public void testUI() throws Throwable
    {
        // ....
         
    // Robotium code begins here
         
    // Wait for the 'Cancel' button
    solo.waitForText(res.getString(R.string.cancel)); 
         
    // We may want to take a screen capture...
    solo.takeScreenshot();
         
    // Click the cancel button
    solo.clickOnText(res.getString(R.string.cancel));
     
    // ....
    }
     
    @Override
    protected void tearDown() throws Exception
    {
        try
        {
            solo.finalize();
        }
        catch(Throwable e)
        {
            // Catch this
        }
        getActivity().finish();
        super.tearDown();
    }
Notice a few things above:
  • We are extending the ActivityInstrumentationTestCase2 class, which provides us with the ability to run test methods on the UI thread.
  • The method that contains your test logic must be prefixed with ‘test’, notice mine is called testUI().
  • We could have looked for the ‘Cancel’ button by passing the text ‘Cancel’ to the waitForText() and clickOnText() methods, but that would only work on the English version of our product. Instead we use the string ID, and look up the value of that string ID based on the current device locale.
  • If you use the ‘takeScreenshot()’ method I have shown an example of above, screen captures will be saved in ‘/sdcard/Robotium-Screenshots/’ on your test device.
Running the Test
Compile the APK containing your test automation script and install it on your test device, along with the APK under test. One ‘gotcha’ here with Robotium is that the APK under test and the APK containing the test logic must be signed with the same certificate. This may not be the case if your APK under test comes out of a build system of some sort. You can use resign.jar to re-sign your APK under test with the same signature as your APK containing the test logic (by running it on the same machine on which you compiled the test APK).
Once both are installed on a test device, we can launch the test from a command prompt via adb:
adb shell am instrument -e class com.jc.robotium.RobotiumExample -w com.jc.robotium/android.test.InstrumentationTestRunner
Conclusion
Robotium is a good test framework for getting automation for Android applications up and running very quickly. It has an active community and updates are released regularly, the latest version (3.6 at time of writing), also seems to be a lot more stable than previous versions.
On the flip side, since we are relying on text resources (which may change frequently), the maintenance on Robotium based automation solutions can be high if your UI changes a lot, which is probably a high possibility for a mobile application.
If your looking for an automation framework for your Android application that you can get up and running quickly, and use to run scripts across an Android application supported in many languages, I would recommend Robotium, and will be keeping a close eye on it as it develops further.
source from http://www.jimmycollins.org/

Comments

Popular posts from this blog

Resigning APK file for automation Testing

Resigning app means we are signing apk file by same signature as we have in our system. Why Resign Sometime for automation of android application, resigning of apk file is required as while working with few automation tools we should have same signature in apk file as well as on the system from which cases need to be triggered What is resigning? All apk file holds complete information about developer of that application For some automation tools, we should have same signature in apk as well as on the system from which cases need to be triggered. For this first we need to generate debug key on our system and then we need to sign apk file using debug key. Process to resign an android application Step 1:  Start Debug Mode in phone and connect with system through USB Step 2:  Check device unique code                Open Command prompt and write  adb devices Step 3:  Now place my application  apk fil...

Handling JSON responses in Apache JMeter

There are various types of   post processor elements   that we can use out of the box when handling responses in a JMeter test plan. For example,   Regular Expression Extractor   can be used to capture a specific value or set of values from a XML, HTML or JSON response. However, handling JSON responses through the default regular expression extractor will be a daunting task when the JSON response becomes complex. When handling complex XML responses, Xpath extractor is the obvious choice in JMeter. Similarly, it will be quite efficient to have a similar post processor element to handle complex JSON responses. JSONPath  is a way to extract parts of a given JSON document and is now available in many programming languages. In this simple post, we will look at how we can use JSONPath expressions to extract values from JSON responses in JMeter. Pre-Requisites: Download and install JMeter version 2.8 or later Step 1 Download  this  JMeter plugin libr...

Mobile Performance Testing: Record Script in JMeter

Before we jump to how to do it we need to understand why to do it. Mobile applications can be classified into below types: Native Apps -  Applications downloaded from online market/store and installed on specific device are categorized as native mobile apps. These applications are developed by using specific programming language (like Java for Android and Objective-C for iOS) and device specific API. These applications are static applications and they do not communicate to sever. Games and applications downloaded from app store or market are examples of native apps. Web Apps - Applications accessed through internet on mobile browsers are called mobile web apps. These applications are developed with web technologies like HTML, JQuery and JavaScript. Popular social media applications like Facebook ( http://m.facebook.com ) and Gmail ( http://m.gmail.com ) etc. have their separate mobile based web apps which are very famous among mobile users. Hybrid Apps-  Combination of web...