Android Test : ActivityInstrumentationTestCase2 : Test 2 buttons, where each one navigates to another activity

Issue

I am trying to make some Android tests for my application. I have a Activity (A) which contains two buttons, each one is navigating to another Activity (B) with a different extra (data) in the intent.

I am able to test one of them, but not both. because when I perform the click of the first, I navigate to the next Activity (B) and the second button is not visible anymore.

My question is : 1- Is it possible to make many case of tests in the same Activity ? 2- Is there a good way or best practice to creat many cases scenarios for test ?
Example —> like I click in the first button , I navigate to Activity (B) and then restart the Activity (A) again, and then click in the second button ?

thanks anyway.

This is the code of the Activitytest :

public class MyActivityTest extends
ActivityInstrumentationTestCase2<MyActivity> {

    /**
    * first button.
    */
    View aButton;

    /**
    * second button.
    */
    View bButton;

    /**
    * Activity under test.
    */
    MyActivity activity;

    /**
    * Create a new MyActivityTest.
    */
    public MyActivityTest() {
        super(MyActivity.class);
    }

    /*
    * (non-Javadoc)
    *
    * @see android.test.ActivityInstrumentationTestCase2#setUp()
    */
    @Override
    protected void setUp() throws Exception {
        super.setUp();
        activity  getActivity();
    }

    /**
    * Tests the behavior when the user selects first button.
    */
    @MediumTest
    public void testClickFirstButton() {

        // ensure a valid handle to the activity has been returned
        assertNotNull(activity);

        aButton  (View) activity
        .findViewById(MyPackage.R.id.first_button);
        assertNotNull(aButton);

        activity.runOnUiThread(new Runnable() {
            public void run() {
                aButton.performClick();
            }
        });

        // wait for the request to go through
        getInstrumentation().waitForIdleSync();
    }

    /**
    * > This test is not running
    * Tests the behavior when the user selects second button.
    */
    @MediumTest
    public void testClickSecondButton() {

        // ensure a valid handle to the activity has been returned
        assertNotNull(activity);

        bButton  (View) activity
        .findViewById(MyPackage.R.id.second_button);
        assertNotNull(bButton);

        activity.runOnUiThread(new Runnable() {
            public void run() {
                bButton.performClick();
            }
        });

        // wait for the request to go through
        getInstrumentation().waitForIdleSync();

    }
}

Solution

I found my solution, but if anyone have a better one it is welcome:

I update the code by overriding the tearDown(), and by using an instance of Solo , to move from a case to another by closing all the opened activities, so I updated as well the setUp() method

@Override
protected void setUp() throws Exception {
    activity  getActivity();
    solo  new Solo(getInstrumentation(), getActivity());
}

@Override
public void tearDown() throws Exception {
    // tearDown() is run after a test case has finished.
    // finishOpenedActivities() will finish all the activities that have
    // been opened during the test execution.
    solo.finishOpenedActivities();
    super.tearDown();
}

so now my test cases are running one by one . the activity is closed and restarted for each case.

from where solo is coming?

You need to add the Robotium dependency:

dependencies {
   // Unit testing dependencies
   androidTestCompile 'com.jayway.android.robotium:robotium:5.4.12'
} 

you can have a look on this tutorial.

Answered By – ahmed_khan_89

Leave a Comment