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.robotiumimport 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
Post a Comment