16

I cant find any good help on this. I have a simple activity with just a few buttons on and I need to test if they re-direct to the correct new page (activity).

public void testButton() {
         button.requestFocus();
         button.performClick();

      }

I really have no idea beyond that. The tutorials are all very unhelpful in doing this :/

Eduardo
  • 551
  • 6
  • 15
user1212520
  • 501
  • 1
  • 5
  • 15

4 Answers4

32

You need ActivityMonitor, it helps you moniotor newly opened activity during instrumentation, check out the pseudo code below:

public void testOpenNextActivity() {
  // register next activity that need to be monitored.
  ActivityMonitor activityMonitor = getInstrumentation().addMonitor(NextActivity.class.getName(), null, false);

  // open current activity.
  MyActivity myActivity = getActivity();
  final Button button = (Button) myActivity.findViewById(com.company.R.id.open_next_activity);
  myActivity.runOnUiThread(new Runnable() {
    @Override
    public void run() {
      // click button and open next activity.
      button.performClick();
    }
  });

  //Watch for the timeout
  //example values 5000 if in ms, or 5 if it's in seconds.
  NextActivity nextActivity = getInstrumentation().waitForMonitorWithTimeout(activityMonitor, 5000);
  // next activity is opened and captured.
  assertNotNull(nextActivity);
  nextActivity .finish();
}
jacktrades
  • 7,224
  • 13
  • 56
  • 83
yorkw
  • 40,926
  • 10
  • 117
  • 130
  • getting error on this line NextActivity nextActivity = getInstrumentation().waitForMonitorWithTimeout(activityMonitor, 5); what is NextActivity??? The Error is : Type mismatch: cannot convert from Activity to ActivityHome – John Jan 28 '15 at 06:29
  • @John NextActivity nextActivity = (NextActivity) getInstrumentation().waitForMonitorWithTimeout(activityMonitor, 5); try to put cast for the next activity will work – Manoj Jan 28 '15 at 07:01
  • @yorkw while calling this line NextActivity nextActivity = getInstrumentation().waitForMonitorWithTimeout(activityMonitor, 5); my next activity comes as null pointer activityMonitor gives as(mResult = null mLastActivity = null mWhich = null mClass = com.activity.Next) how to fix it can u plz guide me – Manoj Jan 28 '15 at 07:14
  • @Manoj Activity nextActivity = getInstrumentation().waitForMonitorWithTimeout(activityMonitor, 5000); // next activity is opened and captured. assertNotNull(nextActivity); nextActivity .finish(); if you remove nextActivity.finish(); the next activity will not close – John Jan 28 '15 at 09:45
  • @Manoj : The Activity is under import android.app.Activity; this Package – John Jan 28 '15 at 09:47
  • Activity nextActivity = getInstrumentation().waitForMonitorWithTimeout(activityMonitor, 5000); // next activity is opened and captured. assertNotNull(nextActivity); – Manoj Jan 28 '15 at 09:56
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/69730/discussion-between-john-and-manoj). – John Jan 28 '15 at 09:57
  • @John Activity nextActivity = getInstrumentation().waitForMonitorWithTimeout(activityMonitor, 5000); // next activity is opened and captured. assertNotNull(nextActivity); nextActivity this gives me null pointer for next activity. – Manoj Jan 28 '15 at 09:57
  • @John can u plz see the chat i had shared my full code which causes null pointer – Manoj Jan 28 '15 at 10:10
11
NextActivity nextActivity = getInstrumentation().waitForMonitorWithTimeout(activityMonitor, 5);

The parameter 5 which is mentioned in above answer's method is in milliseconds not in seconds. So if it is 5, sometimes testcase get failed Because in 5 milliseconds it can't load the next activity. So 5000 or 10000 milliseconds will definitely work better. In documentation they have given it in seconds But in fact it is in milliseconds. So following method will work better than above method.

NextActivity nextActivity = getInstrumentation().waitForMonitorWithTimeout(activityMonitor, 10000);
madth3
  • 7,275
  • 12
  • 50
  • 74
Rohit Haval
  • 139
  • 3
  • 13
0

For an ActivityUnitTestCase test, you can use getStartedActivityIntent() to check for the intent that was passed to a startActivity() call.

protected Intent waitForStartedActivityIntent(int timeout) {
    long endTime = SystemClock.uptimeMillis() + timeout;
    while (SystemClock.uptimeMillis() <= endTime) {
        Intent intent = getStartedActivityIntent();
        if (intent != null) {
            return intent;
        }
    }
    return null;
}

Credit: waitFor... logic was inspired by this answer

Note: with an ActivityUnitTestCase, the activity doesn't actually get started; the call is gobbled up by a mock parent set by the test case.

Community
  • 1
  • 1
ento
  • 5,801
  • 6
  • 51
  • 69
0

You can use ActivityMonitor or Instrumentation to know the target Activity is launched or not like below

public void testButton() {
     Instrumentation.ActivityMonitor activityMonitor = getInstrumentation()
            .addMonitor(TargetActivity.class.getName(), null, false);
    //button.performClick(); // Dont use this method
    TouchUtils.clickView(this, button);
    TargetActivity targetActivity = (TargetActivity) activityMonitor.waitForActivity(); // By using ActivityMonitor
    // TargetActivity targetActivity = (TargetActivity) activityMonitor.waitForActivityWithTimeout(5);// It also works
    // TargetActivity targetActivity = (TargetActivity) getInstrumentation().waitForMonitor(activityMonitor); // By using Instrumentation
   // TargetActivity targetActivity = (TargetActivity) getInstrumentation().waitForMonitorWithTimeout(activityMonitor, 5); // It also works
    assertNotNull("Target Activity is not launched", targetActivity);
  }
Venugopal
  • 1,288
  • 3
  • 19
  • 31