简体   繁体   中英

How to test two activities with Robotium

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

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 (eg click a button which starts a new activity)
  3. wait for the 2nd activity.
  4. do something in the 2nd activity (eg, 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);
        }
    }

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM