Home > General > User scenario testing for Android – Robotium

User scenario testing for Android – Robotium

Robotium is a test framework created to make it easy to write powerful and robust automatic black-box test cases for Android applications. With the support of Robotium, test case developers can write function, system and acceptance test scenarios, spanning multiple Android activities.
Robotium has full support for Activities, Dialogs, Toasts, Menus and Context Menus.
See below for common Robotium questions and answers.

NEWS: Robotium 3.1 is now released!

Robotium 3.1 is lighter, faster and more stable! Now also fully compatible with animation rich applications.

Note that finishOpenedActivities() replaces finalize()!

New functionality includes: finishOpenedActivities(), getView(Class viewClass, int index), waitForText(String text, int minimumNumberOfMatches, long timeout, boolean scroll, boolean onlyVisible), waitForView(View view), waitForView(View view, int timeout, boolean scroll) and more.
Robotium provides the following benefits:

You can develop powerful test cases, with minimal knowledge of the application under test.
The framework handles multiple Android activities automatically.
Minimal time needed to write solid test cases.
Readability of test cases is greatly improved, compared to standard instrumentation tests.
Test cases are more robust due to the run-time binding to GUI components.
Blazing fast test case execution.
Integrates smoothly with Maven or Ant to run tests as part of continuous integration.

Plans for Robotium

Version 1.0 was released in January 2010. At the moment focus is primarily on supporting the community in using Robotium, fixing any issues that come up while continuously working on improving Robotium. As time allows, new features will be developed based on what users express an interest in.
Features we want to implement in the future

Remote control. Have your Robotium test cases run on a PC, in order to improve turn-around times. This is similar to Selenium RC.
Straight-forward integration with Cucumber.
Automatic measurement of user interface test coverage.
Generate screenshots on failure.

Features further down the line

Multidevice support. Write test cases which involve several devices interacting with each other.

Q&A :-
How do I get started with Robotium?

Download the Robotium jar and add it to your test project’s build path. For further instructions go to our getting started page. There you can download a tutorial as well as a test project that can be run through Eclipse.
Which versions of Android does Robotium support?

Robotium officially supports Android 1.6 and up.
Does Robotium support testing on real devices?

Yes it does. One just needs to connect the device to the computer and execute the tests as usual. There can be issues with some vendor customizations of Android, but many times it is possible to get around the incompatibilities by using other Robotium methods.
Which features does Robotium support?

Please download the Javadoc to see a full list of Robotium features. The Javadoc is a jar file that builds on the zip file format. If you do not know how to extract its content you can rename it to .zip instead and extract it with your favourite zip extraction tool.
Is it possible to write a test case that spans over 2 applications?

No, that is not possible. In the AndroidManifest.xml you state which target application you want to test. An example of what it can look like:

That means that the test project is locked to the targetPackage. Going outside of that target package is not allowed by the Android platform. Therefore you will need 2 test projects, one for each application.
Can I use Robotium if I only have the apk file?

Yes you can. You do not need to have the source code. More information is found here.
Can I use Robotium on pre-installed applications?

Yes you can if you have a rooted phone. More information is found here.
Can I use Robotium on flash and web applications?

No. Robotium will not be able to handle the flash or web components, it only works with android components. You could send in clicks by using clickOnScreen() or assert that a certain Activity is shown but that is all. You will not be able to write tests where you verify or work with the web or flash components.
Can I take screenshots from inside of Robotium?

No. To include this functionality an extra security tag will be required in the application under test (that allows of saving the screenshots). All who want to use Robotium would then be required to add that security permission tag to the AndroidManifest.xml of the application under test (even if they do not want to use that functionality). The Robotium users that do not have the possibility to add that security tag to the application under test would be unable to use Robotium. Thus it has not been included into the Robotium API.
How do I run Robotium tests from the command line?

By using this command:

adb shell am instrument -w com.android.foo/android.test.InstrumentationTestRunner

Where com.android.foo is the name of your test project’s package. More on this can be found here.
Why do text and button clicks get wrong?

If this problem happens on one of the supported versions then try to add this tag to the test project’s AndroidManifest.xml

Where YOUR_VERSION is 6 for Android 2.0, 7 for Android 2.1 and 8 for Android 2.2.

If that does not solve the problem then try to add this tag to the AndroidManifest.xml of the application you want to test:

If neither of these solutions solve your problems then write an issue report.
Can I test with localised strings?

Yes you can. You can use solo.getString() and in there pass in the resource id of the string that you want to use. You will need to have the source code of the application under test to access the strings.
How can I get code coverage for my Robotium tests?

You currently need to use ant (or for the perverse, the command line).

Because of security restrictions, you either need root access on the phone or to test in the emulator.

Here are the main steps

run android update test-project -m [path to target application] -p [path to the test folder I’d recommend running this in the root folder of the tests, so -p . You need to run this before you can run the ant coverage command
the Robotium jar needs to be in the libs folder of your test project. e.g. on my windows machine when I’m in the root of my test project I ran the following command copy \opensourceprojects\Robotium\downloads\robotium-solo-2.1.jar libs
ant needs to access to the class files, and libraries of the target application (which assumes you have the source for that application and the ability to build it). For the gist see http://stackoverflow.com/questions/2472059/cant-build-and-run-an-android-test-project-created-using-ant-create-test-projec I have included the extract of my build.xml file below.
run ant coverage
if all’s well the output from ant will tell you a coverage.html has been created in a folder called coverage. You can open that file in a web browser to see the coverage of your Robotium tests 🙂

Here’s the extract of my custom build.xml file.

You should copy and paste this into the current build.xml file for the tests. I’d also recommend creating or editing the build.properties files in the root folders of the tests and the target application with emma.enabled=true

Categories: General
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: