Monday, October 13

Spoon screenshot current activity and change orientation

One of the problems I was having with espresso was to take screenshots of the current activity. With Espresso I can navigate and verify the views on screen, but once I move from one activity to another, the getActivity() function of the ActivityInstrumentationTestCase2 was still returning the activity declared in the class.

I saw that robotium has a function like that, but I wanted to have the same for espresso.

Denys in his post show a simple function to active this http://qathread.blogspot.com.br/2014/09/discovering-espresso-for-android-how-to.html , I will replicate it here:


 public Activity getActivityInstance(){  
   getInstrumentation().runOnMainSync(new Runnable() {  
     public void run() {  
       Collection<Activity> resumedActivities = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED);  
       for (Activity act: resumedActivities){  
         Log.d("Your current activity: ", act.getClass().getName());  
         currentActivity = act;  
         break;  
       }  
     }  
   });  
   
   return currentActivity;  
 }  

So now I can use


 Spoon.screenshot(getActivityInstance(), "screenshot_name");  

Nice =).

Also, if I want to see how the screen looks in landscape, I can do:

 getActivityInstance().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);  

And rotate it back if needed. Cool.

!

No comments:

Post a Comment