Beliebte Suchanfragen

Cloud Native

DevOps

IT-Security

Agile Methoden

Java

//

Android automated testing (Robotium)

17.3.2011 | 2 minutes of reading time

In the previous GWT project we worked with acceptance tests and the Robot Framework. The question I asked myself was if something similar exists for Android. Yes, it exists, and its name is Robotium .
Robotium is a test framework created to write 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. It allows you to create test cases for Android Activities, Dialogs, Toasts, Menus and Context Menus etc.

This is one very simple example how to write Robotium tests:

  1. Create an Android test project if one does not exist (explained earlier in my post: Android testing in brief ).
  2. Create a test class. For example, if you want to test activity “MyAndroidActivity”, your test class then needs to extend android.test.ActivityInstrumentationTestCase2.
  3. 1public class MyAndroidActivityTest extends android.test.ActivityInstrumentationTestCase2 {
  4. Download robotium-solo-xxx.jar library or add a dependency to it in the *.pom file of your maven project. It’s open source at Google Code.
  5. Declare a Solo class instance. When writing tests there is no need to plan for or expect new activities in the test case. All is handled automatically by Robotium-Solo. Robotium-Solo can be used in conjunction with ActivityInstrumentationTestCase2 . The test cases are written from a user perspective were technical details are not needed.
  6. 1private Solo solo;
  7. Create a test class constructor.
  8. 1public MyAndroidActivityTest() {
    2    super("com.example", MyAndroidActivity.class);
    3}
  9. Override the setUp() and tearDown() methods. Overriden  setUp() method is usually the  place where you  instantiate Solo object. In teardDown()  method solo.finalize()  is called and all activites that have been opened are finished.
  10. 1@Override
    2protected void setUp() throws Exception {
    3    super.setUp();
    4    solo = new Solo(getInstrumentation(), getActivity());
    5}
    6 
    7@Override
    8protected void tearDown() throws Exception {
    9    try {
    10        solo.finalize();
    11    } catch (Throwable e) {
    12        e.printStackTrace();
    13    }
    14    getActivity().finish();
    15    super.tearDown();
    16}
  11. Create a test method. Robotium-Solo offers a number of methods for testing various UI events such as: clickOnText(), enterText(), clearEditText, goBack, serachText() etc.
  12. 1public void testDisplayedText() throws InterruptedException {
    2    Assert.assertTrue(solo.searchText("Hello world,my activity"));
    3    Assert.assertTrue(getActivity().getString(R.string.hello_string).equals(solo.getCurrentActivity().getTitle()));
    4}

share post

Likes

0

//

More articles in this subject area

Discover exciting further topics and let the codecentric world inspire you.

//

Gemeinsam bessere Projekte umsetzen.

Wir helfen deinem Unternehmen.

Du stehst vor einer großen IT-Herausforderung? Wir sorgen für eine maßgeschneiderte Unterstützung. Informiere dich jetzt.

Hilf uns, noch besser zu werden.

Wir sind immer auf der Suche nach neuen Talenten. Auch für dich ist die passende Stelle dabei.