繁体   English   中英

Android,Robotium-问题截图

[英]Android, Robotium - Issue taking screenshot

我正在尝试使用Robotium截取我的Android应用程序的屏幕截图,我正在使用在此处找到的以下功能。

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) 
     { 
     } 
} 

我从测试中这样称呼它:

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

当我调用该函数时,在该行上得到一个NullPointerException,它对我来说好像View为null。

我也尝试过使用

solo.getViews();

并循环浏览每个视图并截取屏幕截图,但我也分别获得了NullPointerException。

ArrayList views = solo.getViews();

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

我对使用Robotium进行Android和Android测试自动化足够陌生,有人可以给我一些调试方面的建议吗,或者是Views似乎为空并且我的屏幕截图不起作用的原因?

TIA。

更新

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)

之所以得到NullPointerException,是因为使用了不正确的getView(int id)。 当给它一个索引而不是id时,它将找不到您要查找的视图,因此返回null。 您要使用的是以下内容:

takeScreenShot(solo.getViews()。get(0),“测试”)

这表示在给定时间对Robotium可用的所有视图中的第一个视图。

用于在应用程序的任何位置进行屏幕截图只需编写这段代码

solo.takeScreenshot();

但不要忘记在您的主应用程序中授予权限。

确保您的模拟器为SD卡留出了几兆字节的空间。

如果您想将jpg拉回到您的PC,则可以使用Java运行以下命令行:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM