0

Robotium없이 ActivityInstrumentationTestCase2 만 사용하여 새 작업을 시작하는 버튼을 테스트하려고하는데 잘 작동하지만 다음 테스트 케이스를 실행하려고하면 시작할 수 없습니다. .ActivityInstrumentationTestCase2는 작업 전환 후 새 테스트를 설정할 수 없습니다.

question에 설명 된 바에 따라 다음 활동에서 테스트를 계속하고 클릭 수를 계속할 수 있었지만 여러 기능으로 테스트를 분리하고 싶습니다.

다음 코드를 사용하여 설치 프로그램에서 새로운 작업을 시작하려고했지만 작동하지 않았으며 각 기능에 대해 동일한 작업을 시도했지만 작동하지 않았습니다.

Instrumentation instrumentation = getInstrumentation();  
    Instrumentation.ActivityMonitor monitor = instrumentation.addMonitor(AuthenticateActivity.class.getName(), null, false); 
    Intent intent = new Intent(Intent.ACTION_MAIN); 
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    intent.setClassName(instrumentation.getTargetContext(), AuthenticateActivity.class.getName()); 
    instrumentation.startActivitySync(intent); 
    Activity currentActivity = instrumentation.waitForMonitorWithTimeout(monitor, 5); 

어떻게하면 새로운 활동을 시작하고 다음 테스트 기능을 계속 실행할 수 있습니까?

답변

3

나는 그것을 알아 냈다.

일부 디버그 후 getActivity()를 호출 할 때 두 번째 테스트가 startActivitySync() 메소드에 걸려있는 것으로 나타났습니다. 이전에 시작된 활동을 완료하지 않았기 때문에 발생했습니다. 완료하려면 다음을 수행했습니다.

Button button = testActivity.findViewById(R.id.button); 
    //add the monitor before starting the new activity 
    Instrumentation.ActivityMonitor monitor = getInstrumentation().addMonitor(StartedActivity.class.getName(), null, false); 

    //Clicking this button will open the new Activity 
    TouchUtils.clickView(this, button); 
    getInstrumentation().waitForIdleSync(); 

    //get the started Activity 
    StartedActivity startedActivity = (StartedActivity)instrumentation.waitForMonitor(monitor); 
    //finishing the started activity solved the problem 
    startedActivity.finish();