测试某个按钮开始在Android中的JUnit(不robotium preF)一项新的活动?按钮、测试、Android、preF

2023-09-12 10:19:41 作者:心亡才能忘

我无法找到任何这很好的帮助。我有一个在短短几个按钮一个简单的活动,我需要测试,如果他们重新定向到正确的新页面(活动)。

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 :/

推荐答案

您需要的活动监视器,它可以帮助你在改编期间moniotor新开张的活动,检查出的伪code如下:

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();
}