How to test two activities with Robotium
Asked Answered
C

2

6

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
Coolie answered 15/4, 2013 at 15:25 Comment(0)
G
3

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

  1. start the first activity
  2. do something in the activity (e.g. click a button which starts a new activity)
  3. wait for the 2nd activity.
  4. do something in the 2nd activity (e.g, enter some input text and then click another button)
  5. 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);
        }
    }
Garrot answered 18/7, 2013 at 22:25 Comment(0)
V
1

First, writing logic code in setUp method isn't good idea. I would recommend you to use the fact, that test cases are run in alphabetical order - create one test method with login and then you are logged in your application in rest of them (if that case is run first), so you don't have to sign in before every test method.

About your second question, solo.waitForView(ListView.class) waits for a specified time (I don't remember what is default), but you don't assert it. You should rather use:

assertTrue(solo.waitForView(ListView.class));

However it seems to be a problem with ListView. Make sure you have only one list view on the screen, otherwise you have to use index of that:

solo.clickInList(int line, int index)
Vang answered 15/4, 2013 at 21:7 Comment(2)
Well, as far as I'm concerned tests should be independent from each other so use the fact that they are executed in some order is a bad smell for me.Coolie
Otherwise you have to sign in before every test case and sign out after it. Another option is to check before every test case and do it if you are not.Vang

© 2022 - 2024 — McMap. All rights reserved.