Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 946
Raphael Oliveira Avatar asked Apr 15 '13 15:04

Raphael Oliveira


People also ask

How do you use Robotium?

Test an App with RobotiumStep 1 − Create a test Project in the Android Studio named as “RobotiumTest”. Choose all the default options until you reach to the main page. Step 2 − Copy the Robotium jar file into the Lib folder of the project. Step 3 − Add the dependency in build.

Where can we Execute Robotium Test cases?

Robotium Test cases can be executed on Android Emulator as well as the Real device, we don't need to write any specific configuration code to run Robotium test cases on the Real device. Robotium Can be easily written in the Maven project also, and it can be run through continuous integration tools.

What is Robotium testing tool?

Robotium is an open-source test framework for writing automatic gray box testing cases for Android applications. With the support of Robotium, test case developers can write function, system and acceptance test scenarios, spanning multiple Android activities.


2 Answers

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);
        }
    }
like image 192
droideckar Avatar answered Oct 23 '22 00:10

droideckar


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)
like image 26
maszter Avatar answered Oct 22 '22 23:10

maszter