簡體   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