4

I managed to write two test cases in my XXTest.java with robotium-solo-3.2.1.jar included, luckily in JUnit view it shows the first one is done, which the device exactly worked (on emulator too).

Then it proceed to the second one, but it just hanging there forever! sorry I can't attach screen shot with my account.

here are my code:

    public class XXTest extends ActivityInstrumentationTestCase2<SignInActivity> {

      private Solo solo;
      private Activity mActivity;
      private static final String account = "someone";
      private static final String pwd = "123456";

      @SuppressWarnings("deprecation")
      public XXTest() {
        super("com.acompany.android", SignInActivity.class);
      }

      protected void setUp() throws Exception {
        super.setUp();
        this.mActivity = getActivity();
        solo = new Solo(getInstrumentation(), mActivity);
      }

      @Smoke
      public void testLogIn() throws Exception {

        EditText accountInput = (EditText) solo.getView(R.id.edit_account);
        EditText pwdInput = (EditText) solo.getView(R.id.edit_password);

        solo.clearEditText(accountInput);
        solo.clearEditText(pwdInput);

        solo.enterText(accountInput, account);
        solo.enterText(pwdInput, pwd);
        solo.clickOnButton(mActivity.getResources()
            .getString(R.string.text_sign_in));

        solo.waitForActivity("MainActivity");
        solo.assertCurrentActivity("Expect MainActivity shown...", "MainActivity");
        boolean expected = true;
        boolean actual = solo.searchButton(mActivity.getResources().getString(
            R.string.welcome_dialog_start));
        assertEquals("find friend dialog not shown", expected, actual);
      }

      @Smoke
      public void testOpenPref() throws Exception {

        solo.goBack();
        solo.clickOnMenuItem(mActivity.getResources().getString(
            R.string.text_preferences));
        solo.assertCurrentActivity("Expected PrefActivity..", "PrefActivity");

        solo.goBackToActivity("MainActivity");
        solo.assertCurrentActivity("Expected MainActivity..", "MainActivity");
      }

      protected void tearDown() throws Exception {
        super.tearDown();
      }
}

I've searched the sample of NotePadTest.java from Robotium tutorial, those 3 test cases in it are just work fine!

Please tell me where goes wrong?? Am I missing something somewhere? why the second test case not running?

btw. Can there be more than one class extends ActivityInstrumentationTestCase2 in a test project? curious!

Robert
  • 1,660
  • 22
  • 39

2 Answers2

3

You need to use solo.finishOpenedActivities() in your tearDown().

Renas
  • 1,919
  • 1
  • 18
  • 17
  • Thanks! @Renas, I got it! So all test cases in one test class can only target to the same one android activity, right? what if I want two test cases target on two different activity? – Robert May 04 '12 at 07:06
  • You can target as many activities as you want as long as they belong to the same application. – Renas May 14 '12 at 15:46
  • After adding solo.finishOpenedActivities() in the tearDown() method, the app is sent to the background after the first test method and brought back up and immediately sent to the background again, then nothing happens. Is this the normal way that it is supposed to work? – Balaram Jun 06 '13 at 08:17
  • @Renas http://stackoverflow.com/q/22877538/1503130 I am having an issue here hope you can look into this. – Prateek Apr 05 '14 at 06:55
2

@Robert - this is the issue with Activity testing itself , not to robotium specific .

For the first test method: the basic flow you is like below: 1>in the setUp() method load the main activity (say MainActivity) > do some ops in your testMethod1() - that results to land you in another activity ( say AnotherActivity) > and in tearDown() you kill the launched activity in setUp() method which is MainActivity

note: but AnotherActivity remains live

For the second test method: the basic flow you is like below: 2>in the setUp() method try loading the main activity (say MainActivity) again ,although the previously launched AnotherActivity is not yet got killed, so it hangs there , It doesnt even enter the testMethod2() fr execution -

note: the eclipse graphical runner shows it hangs while the execution marker points to the testMethod2() , people thinks that it got stuck in testMethod2() - but the problem lies in setUp() for testMethod2() not in testMethod2() itself.

Solution: 1>Just keep a look on your cross activity navigation - at the end of each testMethod() use solo.goBack() to come back to the same main activity MainActivity from your current Activity ( you got landed in because of your actions)

results - at the end of testMethod1() only the main activity which was opened in setUP() remains. So it gets killed successfully .No Activity remains alive and for the testMethod2().The setUP() is able to load the MainActivity again without a hang - and testMethod2() gets executed .

hope it clarifies :)

Som
  • 4,618
  • 4
  • 29
  • 32