Android, Robotium - Issue taking screenshot

0
0

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)
  • You must to post comments
0
0

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.

  • You must to post comments
Showing 1 result
Your Answer
Post as a guest by filling out the fields below or if you already have an account.
Name*
E-mail*
Website