2016-06-08 4 views
4

내 프로젝트에서 UI 테스트를 위해 에스프레소를 사용하고 있습니다. 나는 각 활동 (스크린)의 스크린 샷을 찍고 싶다. 스크린 샷을 찍기 위해 GoogleCloudTestLab의 ScreenShooter를 사용하고 있습니다.에스 프레소를 사용하여 스크린 샷 찍기

ScreenShotter.takeScreenshot("main_screen_2", getActivity()); 

하지만 내 ActivityTestRule에서 정의한 첫 번째 활동의 스크린 샷 만 찍습니다. 같은 테스트 케이스에서 다른 활동 스크린 샷을 찍으려면 어떻게해야합니까?

답변

2

필자의 이해는 ActivityTestRule이 테스트 케이스 내에서 하나의 Activity 만 테스트하도록 설계되었으므로 getActivity()는 ActivityTestRule에서 지정한 활동 만 리턴합니다.

이 스크린 샷을 캡처하려면, 라이브러리가 현재 사용

View screenView = activity.getWindow().getDecorView().getRootView(); screenView.setDrawingCacheEnabled(true); Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache()); screenView.setDrawingCacheEnabled(false);

(. 활동이 사용자가 우리에게 전달하는 활동입니다)

그래서 같은 활동이 takescreenshot하기 위해 제공되고 있기 때문에, 그 당시에는 해당 액티비티의 뷰 계층 구조 만 캡처 할 수 있습니다. 테스트 케이스마다 하나의 액티비티 만 테스트하도록 테스트를 나눌 수 있습니까?

또한 우리는 현재 화면을 캡처하는 다른 방법을 모색 중이며이 방법을 변경하면이 스레드에 추가됩니다.

참고 :이 라이브러리를 사용하여 Firebase Test Lab에서 테스트를 실행하고 (라이브러리를 사용하는 대신)/sdcard/screenshots 디렉토리에있는 스크린 샷을 캡처하는 것이 더 좋습니다 그들은 테스트가 끝나면 대시 보드로 끌어 올려집니다.

1

내 테스트에서 여러 활동에 걸쳐있는 흐름을 다루기 때문에 동일한 문제가있었습니다.

/** 
* A helper method to get the currently running activity under test when a test run spans across multiple 
* activities. The {@link android.support.test.rule.ActivityTestRule} only returns the initial activity that 
* was started. 
*/ 
public static final Activity getCurrentActivity(Instrumentation instrumentation) 
{ 
    final Activity[] currentActivity = new Activity[1]; 
    instrumentation.runOnMainSync(new Runnable() 
    { 
     public void run() 
     { 
      Collection<Activity> resumedActivities = 
       ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(RESUMED); 
      if (resumedActivities.iterator().hasNext()) 
      { 
       currentActivity[0] = resumedActivities.iterator().next(); 
      } 
     } 
    }); 
    return currentActivity[0]; 
} 

가 테스트 내에서 getInstrumentation를()을 통과하고 당신이 가서 잘되어야합니다 : A는이 것과 같은 도우미 메서드는 현재 활성화 된 (위쪽) 활동에 대한 참조를 얻을 수 있습니다.