How to test two activities with Robotium

android, automated-tests, robotium

Solution

I was able to test multiple activities in my application by using the following approach:

- start the first activity

- do something in the activity (e.g. click a button which starts a new activity)

- wait for the 2nd activity.

- do something in the 2nd activity (e.g, enter some input text and then click another button)

- etc.

sample code: public void testDisplayBlackBox() {

    //Click on add ident button
    solo.clickOnButton("Tap to get another number");
    if (solo.waitForActivity(IdentityTemplateActivity.class)) {
        // select ident type
        solo.clickOnImageButton(0);

        // add name/label and create ident
        if (solo.waitForActivity(NumberDetailActivity.class)) {
            solo.enterText(0, "Robotium");
            solo.enterText(1, "test 1");    
            solo.clickOnImageButton(6);
        }
    }

Problem

I'm testing my Android application with Robotium and I'm facing one intermittent problem. My application starts with a SigninActivity which allows the user to signin and after that he is directed to the second activity which has a list that is filled after a request to a web server. The first question is: since all my activities can only be accessed after the user is logged in, I need to start every test for every activity from the login screen. So what I'm doing is for every activity test class, I'm inheriting it from ``` ActivityInstrumentationTestCase2<SigninActivity> ``` and in the setUp method I'm loggin in the user. Is this the correct approach? Second question: I want to test the list data in the second activity that is filled after a request to the web server. As mentioned above, in my setup method I login the user, and I use ``` solo.waitForActivity(SecondActivity.class, BIG_TIMEOUT) solo.waitForView(ListView.class) ``` to guarantee that the second activity and the list are present. The problem is, even with this verification I often get ``` junit.framework.AssertionFailedError: Can not click on line number 2 as there are only 0 lines available ```

Original source