Need help to take screen shot

2,259 views
Skip to first unread message

Kantha

unread,
Aug 5, 2010, 2:29:06 AM8/5/10
to Robotium Developers
Hi,
I am using Robotium and it is very helpful for me. But now i m
in a situation where i need to take screenshot of the emulator, i
think currently Robotium dont have any methods or class for taking
screenshot.I have a screenshot class which takes screenshot when
called independently, but when i called that class inside robotium, i
m getting error saying " Test run failed: Process crashed".

Please help me to get out of this.

Thanks

Renas

unread,
Aug 5, 2010, 3:00:51 AM8/5/10
to robotium-...@googlegroups.com
So you can not call the screen shot class from your test case? What does the log say?

/Renas


--
You received this message because you are subscribed to the Google Groups "Robotium Developers" group.
To post to this group, send email to robotium-...@googlegroups.com.
To unsubscribe from this group, send email to robotium-develo...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/robotium-developers?hl=en.


Kantha

unread,
Aug 6, 2010, 4:50:03 AM8/6/10
to Robotium Developers
Yes Renas I am not able to call the Screenshot class.

Below are the logs

I/System.out( 327): 38:08 E/DeviceMonitor: Failed to read the adb
response!
I/System.out( 327): 38:08 E/DeviceMonitor: Adb connection
Error:Failed to read the adb response!
I/System.out( 327): 38:08 E/DeviceMonitor: adb refused request:
closed
I/System.out( 327): 38:08 E/DeviceMonitor: Failed to read the adb
response!
I/System.out( 327): 38:08 E/DeviceMonitor: Adb connection
Error:Failed to read the adb response!
I/System.out( 327): 38:08 E/DeviceMonitor: adb refused request:
closed
I/System.out( 327): 38:08 E/DeviceMonitor: Failed to read the adb
response!
I/System.out( 327): 38:08 E/DeviceMonitor: Adb connection
Error:Failed to read the adb response!
I/System.out( 327): 38:08 E/DeviceMonitor: adb refused request:
closed
I/System.out( 327): 38:08 E/DeviceMonitor: Failed to read the adb
response!
I/System.out( 327): 38:08 E/DeviceMonitor: Adb connection
Error:Failed to read the adb response!
I/System.out( 327): 38:08 E/DeviceMonitor: adb refused request:
closed
I/System.out( 327): 38:08 E/DeviceMonitor: Failed to read the adb
response!
I/System.out( 327): 38:08 E/DeviceMonitor: Adb connection
Error:Failed to read the adb response!
I/System.out( 327): 38:08 E/DeviceMonitor: adb refused request:
closed
I/System.out( 327): 38:08 E/DeviceMonitor: Failed to read the adb
response!
I/System.out( 327): 38:08 E/DeviceMonitor: Adb connection
Error:Failed to read the adb response!
D/dalvikvm( 327): GC_FOR_MALLOC freed 15031 objects / 524544 bytes in
115ms
I/System.out( 327): 38:09 E/DeviceMonitor: adb refused request:
closed
I/System.out( 327): 38:09 E/DeviceMonitor: Failed to read the adb
response!
I/System.out( 327): 38:09 E/DeviceMonitor: Adb connection
Error:Failed to read the adb response!
I/System.out( 327): 38:09 E/DeviceMonitor: adb refused request:
closed
I/System.out( 327): 38:09 E/DeviceMonitor: Failed to read the adb
response!

These logs will come continuously till i close emulator manually.
-----------------------------------------------------------------------------------------------------------------------------


Below is my sample code which calls Screenshot class

public class AIMTest extends ActivityInstrumentationTestCase2{

private static final String TARGET_PACKAGE_ID =
"aol.client.aim";
private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME =
"aol.client.aim.AIM";

private static Class<?> launcherActivityClass;
static{
try {
launcherActivityClass =
Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}

//@SuppressWarnings("unchecked")
public AIMTest() throws ClassNotFoundException {
super(TARGET_PACKAGE_ID, launcherActivityClass);
}

private Solo solo;

protected void setUp() throws Exception {
solo = new Solo(getInstrumentation(), getActivity());
}

//public void testCanOpenSettings(){
// solo.pressMenuItem(0);
// }

public void testAddNote() throws Exception {

Thread.sleep(5000);
solo.clickOnMenuItem("Settings");
Thread.sleep(2000);

new Screenshot(); //calling Screenshot class
}

public void tearDown() throws Exception {

try {
solo.finalize();
} catch (Throwable e) {

e.printStackTrace();
}
getActivity().finish();
super.tearDown();

}

}
---------------------------------------------------------------------------------------------------------------------------------------------------

Below is the Screenshot class



public class Screenshot implements IDeviceChangeListener {

private static final int SCREENSHOT_INTERVAL = 300;

private static boolean mConnected = false;

private static int i=1;

public Screenshot() {
try {
AndroidDebugBridge.init(false);
AndroidDebugBridge adb = AndroidDebugBridge.createBridge();

AndroidDebugBridge.addDeviceChangeListener(this);

System.out.print("Waiting for device connection...\n");
synchronized (this) {
this.wait();
}

IDevice[] devices = adb.getDevices();
if(devices.length > 0) {
getScreenshots(this, devices[0]);
}

System.out.print("Done\n");

} catch (InterruptedException e) {

} finally {
// close the debug bridge
AndroidDebugBridge.terminate();
}
}

private static void getScreenshots(Screenshot ss, IDevice device)
throws InterruptedException {
String thisDevice = device.getSerialNumber();

SimpleDateFormat datefmt = new SimpleDateFormat("yyyyMMdd-HHmmss");
String dir = String.format("%s-%s", thisDevice, datefmt.format(new
Date()));

if (new File(dir).mkdir() == false) {
System.out.printf("Couldn't create directory %s", dir);
return;
}

System.out.printf("Writing files to directory %s\n", dir);

// get images until the device is disconnected
int count = 0;
while (i==1) {
RawImage raw = null;
try {
raw = device.getScreenshot();
} catch (IOException e) {
System.out.printf("Error getting image: %s\n", e.getMessage());
}

if (raw != null) {
String filename = String.format("%s%s%08d.jpg", dir,
File.separator, count);

// save raw data to png
PaletteData pal = new PaletteData(0xf800, 0x07e0, 0x001f);
ImageData image = new ImageData(raw.width, raw.height, raw.bpp,
pal, 1, raw.data);

ImageLoader loader = new ImageLoader();
loader.data = new ImageData[] { image };
loader.save(filename, SWT.IMAGE_PNG);

System.out.printf("Saved picture as %s\n", filename);

count++;

i++;
}

synchronized(ss) {
ss.wait(SCREENSHOT_INTERVAL);
}

}
}

@Override
public void deviceChanged(IDevice device, int changeMask) {
}

@Override
public void deviceConnected(IDevice device) {

System.out.printf("Device %s connected\n",
device.getSerialNumber());
mConnected = true;

synchronized (this) {
notify();
}
}

@Override
public void deviceDisconnected(IDevice device) {

System.out.printf("Device %s disconnected\n",
device.getSerialNumber());
mConnected = false;

synchronized (this) {
notify();
}
}
}


Please give me the solution.





On Aug 5, 12:00 pm, Renas <renasr...@gmail.com> wrote:
> So you can not call the screen shot class from your test case? What does the
> log say?
>
> /Renas
>
> On Thu, Aug 5, 2010 at 8:29 AM, Kantha <kanth...@gmail.com> wrote:
> > Hi,
> >        I am using Robotium and it is very helpful for me. But now i m
> > in a situation where i need to take screenshot of the emulator, i
> > think currently Robotium dont have any methods or class for taking
> > screenshot.I have a screenshot class which takes screenshot when
> > called independently, but when i called that class inside robotium, i
> > m getting error saying " Test run failed: Process crashed".
>
> > Please help me to get out of this.
>
> > Thanks
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Robotium Developers" group.
> > To post to this group, send email to robotium-...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > robotium-develo...@googlegroups.com<robotium-developers%2Bunsu...@googlegroups.com>
> > .

Renas Reda

unread,
Aug 6, 2010, 6:29:42 AM8/6/10
to Robotium Developers
The problem you are experiencing is because of the fact that adb is
busy with the JUnit tests. So therefore it does not accept that you
try to use it (as it is already in use). There is however another way
in which you can take screenshots and save them on the sd card:

public static String SCREEN_SHOTS_LOCATION="/sdcard/";

public static void takeScreenShot(View view) throws Exception
{
takeScreenShot(view, "default");
}

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

//call
takeScreenShot(solo.getViews().get(0), "testname");


/Renas

Kantha

unread,
Aug 7, 2010, 2:51:15 AM8/7/10
to Robotium Developers
Thank you so much Renas.

I have one doubt, is it possible to save the taken screenshot in local
disk?

Because i need to use that screen shot for image comparison.

Or can i use the saved screen shot from SD card for image comparison?

Thanks.
> ...
>
> read more »

Renas Reda

unread,
Aug 7, 2010, 2:59:47 AM8/7/10
to Robotium Developers
You can do a "adb pull" of the screen shot file after the tests are
done. Adb pull will transfer the file from your device to your
computer.

/Renas
> ...
>
> läs mer »

Kantha

unread,
Aug 7, 2010, 11:05:04 PM8/7/10
to Robotium Developers
Thank u Renas, wil try it out..
> ...
>
> read more »

Kantha

unread,
Aug 8, 2010, 2:56:01 AM8/8/10
to Robotium Developers
Hi Renas,

Screenshot code is working fine, it is successfully saving in
sdcard.

Also using "adb pull" I am able to pull the image to my local disk
when used using command prompt, but the problem is when i cal it
inside my code its giving error.

Is it possible to use the commands "adb pull" inside robotium?

Below is my code

public void testAddNote() throws Exception {

Thread.sleep(5000);
solo.clickOnMenuItem("Settings");
Thread.sleep(5000);

takeScreenShot(solo.getViews().get(0), "testAddNote");
Thread.sleep(3000);

String[] command = new String[]{"adb", "pull", "/sdcard/", "C:\\"};
Process child = Runtime.getRuntime().exec(command);

}


Below is the logs

I/TestRunner( 317):
I/TestRunner( 317): java.io.IOException: Error running exec().
Command: [adb, pull, /sdcard/, C:\] Working Directory: null
Environment: null
I/TestRunner( 317): at
java.lang.ProcessManager.exec(ProcessManager.java:226)
I/TestRunner( 317): at java.lang.Runtime.exec(Runtime.java:196)
I/TestRunner( 317): at java.lang.Runtime.exec(Runtime.java:141)
I/TestRunner( 317): at com.aol.AIMTest.testAddNote(AIMTest.java:
78)
I/TestRunner( 317): at
java.lang.reflect.Method.invokeNative(Native Method)
I/TestRunner( 317): at java.lang.reflect.Method.invoke(Method.java:
521)
I/TestRunner( 317): at
android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:
204)
I/TestRunner( 317): at
android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:
194)
I/TestRunner( 317): at
android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:
186)
I/TestRunner( 317): at
junit.framework.TestCase.runBare(TestCase.java:127)
I/TestRunner( 317): at junit.framework.TestResult
$1.protect(TestResult.java:106)
I/TestRunner( 317): at
junit.framework.TestResult.runProtected(TestResult.java:124)
I/TestRunner( 317): at
junit.framework.TestResult.run(TestResult.java:109)
I/TestRunner( 317): at junit.framework.TestCase.run(TestCase.java:
118)
I/TestRunner( 317): at
android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
I/TestRunner( 317): at
android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
I/TestRunner( 317): at
android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:
520)
I/TestRunner( 317): at android.app.Instrumentation
$InstrumentationThread.run(Instrumentation.java:1447)
I/TestRunner( 317): Caused by: java.io.IOException: Permission denied
I/TestRunner( 317): at java.lang.ProcessManager.exec(Native
Method)
I/TestRunner( 317): at
java.lang.ProcessManager.exec(ProcessManager.java:224)
I/TestRunner( 317): ... 17 more

Please give me a solution.

Thanks


On Aug 7, 11:59 am, Renas Reda <renasr...@gmail.com> wrote:
> ...
>
> read more »

Renas Reda

unread,
Aug 8, 2010, 4:26:05 AM8/8/10
to Robotium Developers
Hi,

No you can not use it inside of Robotium. What you can do is create a
small script (e.g. bash script) that not only runs the tests by the
use of "adb instrument ...." but also after the tests are done does an
adb pull.

/Renas
> ...
>
> read more »

ayushi...@gmail.com

unread,
Aug 8, 2010, 10:09:49 AM8/8/10
to Robotium Developers
Hi, i also need to take screenshots for testing my application. please
tell me how to proceed.

Kantha

unread,
Aug 9, 2010, 12:08:50 AM8/9/10
to Robotium Developers
Once again thank u Renas.

But actually what i thought is compare the taken screen shot image
with the expected image, if it matches testcase should pass else it
should fail.
So can i call image comparison class inside robotium by passing image
which is stored in sd card and the expected image which is there in my
local disk?

Please guide me, how can i move forward? which is better to do?

Thanks
> ...
>
> read more »

Fabrizio Giudici

unread,
Aug 9, 2010, 3:22:40 AM8/9/10
to robotium-...@googlegroups.com, Kantha

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 8/9/10 06:08 , Kantha wrote:
> Once again thank u Renas.
>
> But actually what i thought is compare the taken screen shot image
> with the expected image, if it matches testcase should pass else it
> should fail.
> So can i call image comparison class inside robotium by passing image
> which is stored in sd card and the expected image which is there in my
> local disk?
>
> Please guide me, how can i move forward? which is better to do?
>

I think that it's more convenient to do the comparison in JAva code
that doesn't run inside Android, because it's easier - and you usually
can't rely on a direct pixel-by-pixel comparison - I'm telling you
this because, even though I've never done this so far, I've recently
talked with a guy who's experienced in this task, and he told me that
he first subtracts the two images and then runs a filter on the result
that tolerates some slight changes. I'd say that in plain Java there
are more readily available imaging libraries (such as JAI) that should
make the task a matter of a few lines of code.

- --
Fabrizio Giudici - Java Architect, Project Manager
Tidalwave s.a.s. - "We make Java work. Everywhere."
java.net/blog/fabriziogiudici - www.tidalwave.it/people
Fabrizio...@tidalwave.it
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.14 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkxfrMAACgkQeDweFqgUGxcRSwCdFcL56ePLujC3MsKaYKgYMK9G
vKgAoJcexAUeyHqml5D8y+MZFu6WuC+f
=EJYQ
-----END PGP SIGNATURE-----

Kantha

unread,
Aug 9, 2010, 8:21:44 AM8/9/10
to Robotium Developers, fabrizio...@tidalwave.it
Thanks Fabrizio for your inputs..

Outside android my comparison code is working good but i want to
include this as part of my test so that i would get result whether
test passed or failed depending on this comparison.

Or is it possible to take screenshot on test failure?

Thanks



On Aug 9, 12:22 pm, Fabrizio Giudici <fabrizio.giud...@tidalwave.it>
wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On 8/9/10 06:08 , Kantha wrote:> Once again thank u Renas.
>
> > But actually what i thought is compare the taken screen shot image
> > with the expected image, if it matches testcase should pass else it
> > should fail.
> > So can i call image comparison class inside robotium by passing image
> > which is stored in sd card and the expected image which is there in my
> > local disk?
>
> > Please guide me, how can i move forward? which is better to do?
>
> I think that it's more convenient to do the comparison in JAva code
> that doesn't run inside Android, because it's easier - and you usually
> can't rely on a direct pixel-by-pixel comparison - I'm telling you
> this because, even though I've never done this so far, I've recently
> talked with a guy who's experienced in this task, and he told me that
> he first subtracts the two images and then runs a filter on the result
> that tolerates some slight changes. I'd say that in plain Java there
> are more readily available imaging libraries (such as JAI) that should
> make the task a matter of a few lines of code.
>
> - --
> Fabrizio Giudici - Java Architect, Project Manager
> Tidalwave s.a.s. - "We make Java work. Everywhere."
> java.net/blog/fabriziogiudici -www.tidalwave.it/people
> Fabrizio.Giud...@tidalwave.it
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG/MacGPG2 v2.0.14 (Darwin)
> Comment: Using GnuPG with Mozilla -http://enigmail.mozdev.org/

Fabrizio Giudici

unread,
Jun 12, 2013, 3:27:27 AM6/12/13
to robotium-...@googlegroups.com, Nidhi Agarwal, fabrizio...@tidalwave.it
On Wed, 12 Jun 2013 07:54:41 +0200, Nidhi Agarwal
<nidhiaga...@gmail.com> wrote:

> Hi LakshmiKantha, Fabrizio
>
> I am capturing snapshot using solo.takeScreenshot API. But for some
> reason
> when I compare it with a previously captured screenshot(using same API
> for
> the same screen on the same emulator/device) my image comparison always
> returns false. I was using Bitmap.sameAs API for comparison.
>
> However when I capture snapshot using cache - the comparison returns true
> for same image. Why is the difference when the expected image and actual
> image both are captured using Robotium API i.e. solo.takeScreenshot?
>
> Any help in this regard will be great.

Hi all - unfortunately I can't help much in this area - my original email
was three years old and in recent time I've not worked in this area with
Robotium... so I don't recall what I was doing :-(


--
Fabrizio Giudici - Java Architect @ Tidalwave s.a.s.
"We make Java work. Everywhere."
http://tidalwave.it/fabrizio/blog - fabrizio...@tidalwave.it
Reply all
Reply to author
Forward
0 new messages