robotium: an introduction

Sunday, 18 May 2014 07:19

Robotium is a test framework for automated testing of Android applications by black box method. Robotium allows you to create scripts for functional, system and acceptance testing.

Simple test on Robotium:

Before creating a test, create a new project in Eclipse IDE:

  1. Create a new project (File->New->Java Project)
  2. Create a package under this project (com.tests)
  3. Add robotium-solo-xxx.jar to Java Build Path
  4. Right click on the project
  5. Click “Properties”
  6. Click Java Build Path
  7. Click Libraries
  8. Click Add External Libraries
  9. Select robotium-solo-xxx.jar
  10. Create test class under the package from step 2 (Example.java)

So, let’s start writing our first test:

 
public class Example extends ActivityInstrumentationTestCase2 {
 
declare our class private Solo solo; declare a class object Solo
 
public EditorTest() {
    super(“com.test.editor”, EditorActivity.class);
}

declare the constructor of our class
 
@Override
public void setUp() throws Exception {
    solo = new Solo(getInstrumentation(), getActivity());
}

@Override
public void tearDown() throws Exception {
    try {
        solo.finalize();
    } catch (Throwable e) {
        e.printStackTrace();
    }

    getActivity().finish();
    super.tearDown();
}
 

override setUp() and tearDown(), which will be run before performing the test and after the test, respectively.

public void testSomething() throws Exception {
    solo.sendKey(Solo.MENU);
    solo.clickOnText(“More”);
    solo.clickOnText(“Preferences”);
    solo.clickOnText(“Edit”);
    Assert.assertTrue(solo.searchText(“hello”));
}

directly the actual test.

As we can see, everything is simple!

Useful links: http://code.google.com/p/robotium/

100.000 visitors per second? the story behind a startup in the cave of lions More