Android, Robotium - Issue taking screenshot

允我心安 提交于 2019-12-01 09:18:15

问题


I'm trying to take a screenshot of my Android application using Robotium, I'm using the below function that I found here.

public static String SCREEN_SHOTS_LOCATION="/sdcard/"; 

public static void takeScreenShot(View view, String name) throws Exception 
{ 
    view.setDrawingCacheEnabled(true); 
    view.buildDrawingCache(); 
    Bitmap b = view.getDrawingCache(); 
    FileOutputStream fos = null; 

    try 
    { 
         File sddir = new File(SCREEN_SHOTS_LOCATION); 

         if (!sddir.exists()) 
         { 
             sddir.mkdirs(); 
         } 
         fos = new FileOutputStream(SCREEN_SHOTS_LOCATION + name + "_" + System.currentTimeMillis() + ".jpg"); 

         if (fos != null) 
         { 
             b.compress(Bitmap.CompressFormat.JPEG, 90, fos); 
             fos.close(); 
         } 
     } 
     catch (Exception e) 
     { 
     } 
} 

I'm calling it like this from my test:

takeScreenShot(solo.getView(0), "Test");

When I call the function, I get a NullPointerException on that line, it looks to me as if the View is null.

I've also tried using

solo.getViews();

and cycling through each view and taking a screenshot, but I get a NullPointerException for each also.

ArrayList views = solo.getViews();

for(int i=0; i < views.size(); i++)
{
    takeScreenShot(solo.getView(i), "Test");
}

I'm new enough to Android & Android test automation using Robotium, can anybody give me some advice on debugging this, or the reason why Views seem to be null and my screen captures don't work?

TIA.

Update

Error in testUI:
java.lang.NullPointerException
        at com.myapp.test.UITests.testUI(UITests.java:117)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:204)
        at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:194)
        at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:186)
        at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
        at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
        at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
        at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448)

回答1:


The reason why you are getting NullPointerException is because you are using getView(int id) incorrectly. As you are giving it an index instead of id, it will not find the view that you are looking for and thus returns null. What you want to use is following:

takeScreenShot(solo.getViews().get(0), "Test")

Which means the first view of all the views available to Robotium at a given time.




回答2:


Make sure your emulator has some megabytes set aside for the SD card.

If you want to pull the jpg back to your PC, you can get java to run this command line:

C:\Users\Me\android-sdks\platform-tools\adb.exe pull /sdcard/test_1329402481933.jpg c:\




回答3:


For taking screen shot at any point of the application Just write this piece of code

solo.takeScreenshot();

But don't forget to give permission in your main Application.



来源:https://stackoverflow.com/questions/8819542/android-robotium-issue-taking-screenshot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!