Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » SWTBot » Improvement for captureScreenshot
Improvement for captureScreenshot [message #25499] Tue, 03 March 2009 16:09 Go to next message
Etienne  is currently offline Etienne Friend
Messages: 13
Registered: July 2009
Junior Member
Hi Ketan,

Thanks so much for SWTBot; I'm using it for a couple of weeks, and it
gives me a lot of satisfaction.

The project I'm working on has a GUI which is changing from day to day,
because it's for research purposes. I have thought SWTbot could also
helped me to maintain up to date the screenshots of the documentation.

Unfortunately, it is not possible to take the screenshoot of a single
shell, or tabfolder. Would it be possible to add a method like this one?

public static boolean captureScreenshot(final String fileName, Widget
widget)

Regards,

Etienne
Re: Improvement for captureScreenshot [message #25537 is a reply to message #25499] Tue, 03 March 2009 16:52 Go to previous messageGo to next message
Ketan Padegaonkar is currently offline Ketan PadegaonkarFriend
Messages: 873
Registered: July 2009
Senior Member
Hi Etienne,

This is very much possible. Take a look at
SWTUtils#captureScreenshotInternal for how the screenshot works. It
works by taking the entire display and dumping the content into an image
object.

What you need instead to do to take the widget instead of the display,
and you should be done.

-- Ketan

On 3/3/09 21:39, Etienne wrote:
> Hi Ketan,
>
> Thanks so much for SWTBot; I'm using it for a couple of weeks, and it
> gives me a lot of satisfaction.
>
> The project I'm working on has a GUI which is changing from day to day,
> because it's for research purposes. I have thought SWTbot could also
> helped me to maintain up to date the screenshots of the documentation.
>
> Unfortunately, it is not possible to take the screenshoot of a single
> shell, or tabfolder. Would it be possible to add a method like this one?
>
> public static boolean captureScreenshot(final String fileName, Widget
> widget)
>
> Regards,
>
> Etienne
>
>
Re: Improvement for captureScreenshot [message #26049 is a reply to message #25537] Wed, 04 March 2009 14:32 Go to previous messageGo to next message
Cedric Brun is currently offline Cedric BrunFriend
Messages: 431
Registered: July 2009
Senior Member
Hi,

here is a sample usage of SWTBot to generate the documentation images:

http://model-driven-blogging.blogspot.com/2009/03/shuangxi-e ffect-ui-
testing-and.html

and here is the code to capture the "active shell screenshot"

/**
* This captures a screen shot and saves it to the given file.
*
* @param fileName
* the filename to save screenshot to.
* @return <code>true</code> if the screenshot was created and saved,
* <code>false</code> otherwise.
* @since 1.0
*/
public static boolean captureScreenshot(final String fileName) {
new ImageFormatConverter().imageTypeOf(fileName.substring(fileNa me
.lastIndexOf('.') + 1));
return UIThreadRunnable.syncExec(new BoolResult() {
public Boolean run() {
return captureScreenshotInternal("screenshots/"+fileName,
Display.getDefault()
.getActiveShell());
}
});
}

/**
* Captures a screen shot. Used internally.
* <p>
* NOTE: This method is not thread safe. Clients must ensure that they do
* invoke this from a UI thread.
* </p>
*
* @param fileName
* the filename to save screenshot to.
* @param widget
* @return <code>true</code> if the screenshot was created and saved,
* <code>false</code> otherwise.
* @since 1.1
*/
protected static boolean captureScreenshotInternal(final String fileName,
Shell widget) {
GC gc = new GC(Display.getDefault());
Image image = null;
try {
Rectangle bounds = widget.getBounds();
int width = bounds.width;
int height = bounds.height;

image = new Image(Display.getDefault(), width, height);
gc.copyArea(image, bounds.x, bounds.y);
ImageLoader imageLoader = new ImageLoader();
imageLoader.data = new ImageData[] { image.getImageData() };
imageLoader.save(fileName, new ImageFormatConverter()
.imageTypeOf(fileName
.substring(fileName.lastIndexOf('.') + 1)));
return true;
} catch (Exception e) {

File brokenImage = new File(fileName).getAbsoluteFile();
if (brokenImage.exists()) {
try {

brokenImage.deleteOnExit();
} catch (Exception ex) {
//
}
}
return false;
} finally {
gc.dispose();
if (image != null) {
image.dispose();
}
}
}


cheers,

Cédric
Ketan Padegaonkar wrote:

> Hi Etienne,
>
> This is very much possible. Take a look at
> SWTUtils#captureScreenshotInternal for how the screenshot works. It
> works by taking the entire display and dumping the content into an image
> object.
>
> What you need instead to do to take the widget instead of the display,
> and you should be done.
>
> -- Ketan
>
> On 3/3/09 21:39, Etienne wrote:
>> Hi Ketan,
>>
>> Thanks so much for SWTBot; I'm using it for a couple of weeks, and it
>> gives me a lot of satisfaction.
>>
>> The project I'm working on has a GUI which is changing from day to day,
>> because it's for research purposes. I have thought SWTbot could also
>> helped me to maintain up to date the screenshots of the documentation.
>>
>> Unfortunately, it is not possible to take the screenshoot of a single
>> shell, or tabfolder. Would it be possible to add a method like this one?
>>
>> public static boolean captureScreenshot(final String fileName, Widget
>> widget)
>>
>> Regards,
>>
>> Etienne
>>
>>


http://cedric.brun.io news and articles on eclipse and eclipse modeling.
Re: Improvement for captureScreenshot [message #26132 is a reply to message #26049] Wed, 04 March 2009 16:50 Go to previous messageGo to next message
Etienne  is currently offline Etienne Friend
Messages: 13
Registered: July 2009
Junior Member
Thank you very much, Ketan and Cédric,

I have made something which looks like Cédric's code. I have just made
things a bit more generic, with parameter Control instead of Shell.
(my code below)

Etienne



protected static boolean captureScreenshotInternal(final String
fileName, final Control control) {
GC gc = new GC(display());
Image image = null;
try {
//Rectangle bounds = display().getBounds();
//int width = bounds.width;
//int height = bounds.height;

int width = control.getBounds().width;
int height = control.getBounds().height;
Point p = control.toDisplay(0, 0);

image = new Image(display(), width, height);
gc.copyArea(image, p.x, p.y);

(...) // No more modifications after this line.




Cédric Brun a écrit :
> Hi,
>
> here is a sample usage of SWTBot to generate the documentation images:
>
> http://model-driven-blogging.blogspot.com/2009/03/shuangxi-e ffect-ui-
> testing-and.html
>
> and here is the code to capture the "active shell screenshot"
>
> /**
> * This captures a screen shot and saves it to the given file.
> *
> * @param fileName
> * the filename to save screenshot to.
> * @return <code>true</code> if the screenshot was created and saved,
> * <code>false</code> otherwise.
> * @since 1.0
> */
> public static boolean captureScreenshot(final String fileName) {
> new ImageFormatConverter().imageTypeOf(fileName.substring(fileNa me
> .lastIndexOf('.') + 1));
> return UIThreadRunnable.syncExec(new BoolResult() {
> public Boolean run() {
> return captureScreenshotInternal("screenshots/"+fileName,
> Display.getDefault()
> .getActiveShell());
> }
> });
> }
>
> /**
> * Captures a screen shot. Used internally.
> * <p>
> * NOTE: This method is not thread safe. Clients must ensure that they do
> * invoke this from a UI thread.
> * </p>
> *
> * @param fileName
> * the filename to save screenshot to.
> * @param widget
> * @return <code>true</code> if the screenshot was created and saved,
> * <code>false</code> otherwise.
> * @since 1.1
> */
> protected static boolean captureScreenshotInternal(final String fileName,
> Shell widget) {
> GC gc = new GC(Display.getDefault());
> Image image = null;
> try {
> Rectangle bounds = widget.getBounds();
> int width = bounds.width;
> int height = bounds.height;
>
> image = new Image(Display.getDefault(), width, height);
> gc.copyArea(image, bounds.x, bounds.y);
> ImageLoader imageLoader = new ImageLoader();
> imageLoader.data = new ImageData[] { image.getImageData() };
> imageLoader.save(fileName, new ImageFormatConverter()
> .imageTypeOf(fileName
> .substring(fileName.lastIndexOf('.') + 1)));
> return true;
> } catch (Exception e) {
>
> File brokenImage = new File(fileName).getAbsoluteFile();
> if (brokenImage.exists()) {
> try {
>
> brokenImage.deleteOnExit();
> } catch (Exception ex) {
> //
> }
> }
> return false;
> } finally {
> gc.dispose();
> if (image != null) {
> image.dispose();
> }
> }
> }
>
>
> cheers,
>
> Cédric
> Ketan Padegaonkar wrote:
>
>> Hi Etienne,
>>
>> This is very much possible. Take a look at
>> SWTUtils#captureScreenshotInternal for how the screenshot works. It
>> works by taking the entire display and dumping the content into an image
>> object.
>>
>> What you need instead to do to take the widget instead of the display,
>> and you should be done.
>>
>> -- Ketan
>>
>> On 3/3/09 21:39, Etienne wrote:
>>> Hi Ketan,
>>>
>>> Thanks so much for SWTBot; I'm using it for a couple of weeks, and it
>>> gives me a lot of satisfaction.
>>>
>>> The project I'm working on has a GUI which is changing from day to day,
>>> because it's for research purposes. I have thought SWTbot could also
>>> helped me to maintain up to date the screenshots of the documentation.
>>>
>>> Unfortunately, it is not possible to take the screenshoot of a single
>>> shell, or tabfolder. Would it be possible to add a method like this one?
>>>
>>> public static boolean captureScreenshot(final String fileName, Widget
>>> widget)
>>>
>>> Regards,
>>>
>>> Etienne
>>>
>>>
>
>
Re: Improvement for captureScreenshot [message #26174 is a reply to message #26049] Wed, 04 March 2009 17:22 Go to previous messageGo to next message
Ketan Padegaonkar is currently offline Ketan PadegaonkarFriend
Messages: 873
Registered: July 2009
Senior Member
Hi,

Could you attach this to the bugzilla, and I could add this as an API to
SWTBot :)

-- Ketan

On 4/3/09 20:02, Cédric Brun wrote:
> Hi,
>
> here is a sample usage of SWTBot to generate the documentation images:
>
> http://model-driven-blogging.blogspot.com/2009/03/shuangxi-e ffect-ui-
> testing-and.html
>
> and here is the code to capture the "active shell screenshot"
>
> /**
> * This captures a screen shot and saves it to the given file.
> *
> * @param fileName
> * the filename to save screenshot to.
> * @return<code>true</code> if the screenshot was created and saved,
> *<code>false</code> otherwise.
> * @since 1.0
> */
> public static boolean captureScreenshot(final String fileName) {
> new ImageFormatConverter().imageTypeOf(fileName.substring(fileNa me
> .lastIndexOf('.') + 1));
> return UIThreadRunnable.syncExec(new BoolResult() {
> public Boolean run() {
> return captureScreenshotInternal("screenshots/"+fileName,
> Display.getDefault()
> .getActiveShell());
> }
> });
> }
>
> /**
> * Captures a screen shot. Used internally.
> *<p>
> * NOTE: This method is not thread safe. Clients must ensure that they do
> * invoke this from a UI thread.
> *</p>
> *
> * @param fileName
> * the filename to save screenshot to.
> * @param widget
> * @return<code>true</code> if the screenshot was created and saved,
> *<code>false</code> otherwise.
> * @since 1.1
> */
> protected static boolean captureScreenshotInternal(final String fileName,
> Shell widget) {
> GC gc = new GC(Display.getDefault());
> Image image = null;
> try {
> Rectangle bounds = widget.getBounds();
> int width = bounds.width;
> int height = bounds.height;
>
> image = new Image(Display.getDefault(), width, height);
> gc.copyArea(image, bounds.x, bounds.y);
> ImageLoader imageLoader = new ImageLoader();
> imageLoader.data = new ImageData[] { image.getImageData() };
> imageLoader.save(fileName, new ImageFormatConverter()
> .imageTypeOf(fileName
> .substring(fileName.lastIndexOf('.') + 1)));
> return true;
> } catch (Exception e) {
>
> File brokenImage = new File(fileName).getAbsoluteFile();
> if (brokenImage.exists()) {
> try {
>
> brokenImage.deleteOnExit();
> } catch (Exception ex) {
> //
> }
> }
> return false;
> } finally {
> gc.dispose();
> if (image != null) {
> image.dispose();
> }
> }
> }
>
>
> cheers,
>
> Cédric
> Ketan Padegaonkar wrote:
>
>> Hi Etienne,
>>
>> This is very much possible. Take a look at
>> SWTUtils#captureScreenshotInternal for how the screenshot works. It
>> works by taking the entire display and dumping the content into an image
>> object.
>>
>> What you need instead to do to take the widget instead of the display,
>> and you should be done.
>>
>> -- Ketan
>>
>> On 3/3/09 21:39, Etienne wrote:
>>> Hi Ketan,
>>>
>>> Thanks so much for SWTBot; I'm using it for a couple of weeks, and it
>>> gives me a lot of satisfaction.
>>>
>>> The project I'm working on has a GUI which is changing from day to day,
>>> because it's for research purposes. I have thought SWTbot could also
>>> helped me to maintain up to date the screenshots of the documentation.
>>>
>>> Unfortunately, it is not possible to take the screenshoot of a single
>>> shell, or tabfolder. Would it be possible to add a method like this one?
>>>
>>> public static boolean captureScreenshot(final String fileName, Widget
>>> widget)
>>>
>>> Regards,
>>>
>>> Etienne
>>>
>>>
>
>
Re: Improvement for captureScreenshot [message #26257 is a reply to message #26174] Thu, 05 March 2009 13:05 Go to previous messageGo to next message
Etienne  is currently offline Etienne Friend
Messages: 13
Registered: July 2009
Junior Member
No problem, I'm going to add it to bugzilla.

Etienne


Ketan Padegaonkar a écrit :
> Hi,
>
> Could you attach this to the bugzilla, and I could add this as an API to
> SWTBot :)
>
> -- Ketan
>
> On 4/3/09 20:02, Cédric Brun wrote:
>> Hi,
>>
>> here is a sample usage of SWTBot to generate the documentation images:
>>
>> http://model-driven-blogging.blogspot.com/2009/03/shuangxi-e ffect-ui-
>> testing-and.html
>>
>> and here is the code to capture the "active shell screenshot"
>>
>> /**
>> * This captures a screen shot and saves it to the given file.
>> *
>> * @param fileName
>> * the filename to save screenshot to.
>> * @return<code>true</code> if the screenshot was created and saved,
>> *<code>false</code> otherwise.
>> * @since 1.0
>> */
>> public static boolean captureScreenshot(final String fileName) {
>> new
>> ImageFormatConverter().imageTypeOf(fileName.substring(fileNa me
>> .lastIndexOf('.') + 1));
>> return UIThreadRunnable.syncExec(new BoolResult() {
>> public Boolean run() {
>> return captureScreenshotInternal("screenshots/"+fileName,
>> Display.getDefault()
>> .getActiveShell());
>> }
>> });
>> }
>>
>> /**
>> * Captures a screen shot. Used internally.
>> *<p>
>> * NOTE: This method is not thread safe. Clients must ensure that
>> they do
>> * invoke this from a UI thread.
>> *</p>
>> *
>> * @param fileName
>> * the filename to save screenshot to.
>> * @param widget
>> * @return<code>true</code> if the screenshot was created and saved,
>> *<code>false</code> otherwise.
>> * @since 1.1
>> */
>> protected static boolean captureScreenshotInternal(final String
>> fileName,
>> Shell widget) {
>> GC gc = new GC(Display.getDefault());
>> Image image = null;
>> try {
>> Rectangle bounds = widget.getBounds();
>> int width = bounds.width;
>> int height = bounds.height;
>>
>> image = new Image(Display.getDefault(), width, height);
>> gc.copyArea(image, bounds.x, bounds.y);
>> ImageLoader imageLoader = new ImageLoader();
>> imageLoader.data = new ImageData[] { image.getImageData() };
>> imageLoader.save(fileName, new ImageFormatConverter()
>> .imageTypeOf(fileName
>> .substring(fileName.lastIndexOf('.') + 1)));
>> return true;
>> } catch (Exception e) {
>>
>> File brokenImage = new File(fileName).getAbsoluteFile();
>> if (brokenImage.exists()) {
>> try {
>>
>> brokenImage.deleteOnExit();
>> } catch (Exception ex) {
>> //
>> }
>> }
>> return false;
>> } finally {
>> gc.dispose();
>> if (image != null) {
>> image.dispose();
>> }
>> }
>> }
>>
>>
>> cheers,
>>
>> Cédric
>> Ketan Padegaonkar wrote:
>>
>>> Hi Etienne,
>>>
>>> This is very much possible. Take a look at
>>> SWTUtils#captureScreenshotInternal for how the screenshot works. It
>>> works by taking the entire display and dumping the content into an image
>>> object.
>>>
>>> What you need instead to do to take the widget instead of the display,
>>> and you should be done.
>>>
>>> -- Ketan
>>>
>>> On 3/3/09 21:39, Etienne wrote:
>>>> Hi Ketan,
>>>>
>>>> Thanks so much for SWTBot; I'm using it for a couple of weeks, and it
>>>> gives me a lot of satisfaction.
>>>>
>>>> The project I'm working on has a GUI which is changing from day to day,
>>>> because it's for research purposes. I have thought SWTbot could also
>>>> helped me to maintain up to date the screenshots of the documentation.
>>>>
>>>> Unfortunately, it is not possible to take the screenshoot of a single
>>>> shell, or tabfolder. Would it be possible to add a method like this
>>>> one?
>>>>
>>>> public static boolean captureScreenshot(final String fileName, Widget
>>>> widget)
>>>>
>>>> Regards,
>>>>
>>>> Etienne
>>>>
>>>>
>>
>>
>
Re: Improvement for captureScreenshot [message #26376 is a reply to message #26257] Thu, 05 March 2009 15:13 Go to previous messageGo to next message
Chris Aniszczyk is currently offline Chris AniszczykFriend
Messages: 674
Registered: July 2009
Senior Member
Etienne wrote:
> No problem, I'm going to add it to bugzilla.

Do you have a bug number?

Cheers,

Chris Aniszczyk | EclipseSource Austin | +1 860 839 2465
http://twitter.com/eclipsesource | http://twitter.com/caniszczyk
Re: Improvement for captureScreenshot [message #26447 is a reply to message #26376] Thu, 05 March 2009 16:44 Go to previous messageGo to next message
Ketan Padegaonkar is currently offline Ketan PadegaonkarFriend
Messages: 873
Registered: July 2009
Senior Member
https://bugs.eclipse.org/bugs/show_bug.cgi?id=267189

Fixed and committed to trunk, as some added goodness, you can now
capture just about any rectangular region on the screen.

-- Ketan

On 5/3/09 20:43, Chris Aniszczyk wrote:
> Etienne wrote:
>> No problem, I'm going to add it to bugzilla.
>
> Do you have a bug number?
>
> Cheers,
>
> Chris Aniszczyk | EclipseSource Austin | +1 860 839 2465
> http://twitter.com/eclipsesource | http://twitter.com/caniszczyk
Re: Improvement for captureScreenshot [message #26984 is a reply to message #26447] Tue, 10 March 2009 10:14 Go to previous message
Etienne  is currently offline Etienne Friend
Messages: 13
Registered: July 2009
Junior Member
After some other tests, I have found a problem in my implementation.

In method :
public static boolean captureScreenshot(final String fileName, final
Control control)

The line :
Point p = control.toDisplay(0, 0);

Gives good coordinates for widgets *except* for shells which have a border.
In this special case, the toDisplay method returns the first point on
the top left corner inside the borders of the wigets, whereas we would
like to get the point on the top left corner of the System title bar of
the window.

I think we could change the line :
Point p = control.toDisplay(0, 0);

into :

if (control.getClass() == Shell.class)
p = control.getLocation();
else
p = control.toDisplay(0, 0);


I'm not expert enough in SWT to say if it's the best way to get correct
screenshot areas...

Etienne

--> https://bugs.eclipse.org/bugs/show_bug.cgi?id=267189



Ketan Padegaonkar a écrit :
> https://bugs.eclipse.org/bugs/show_bug.cgi?id=267189
>
> Fixed and committed to trunk, as some added goodness, you can now
> capture just about any rectangular region on the screen.
>
> -- Ketan
>
> On 5/3/09 20:43, Chris Aniszczyk wrote:
>> Etienne wrote:
>>> No problem, I'm going to add it to bugzilla.
>>
>> Do you have a bug number?
>>
>> Cheers,
>>
>> Chris Aniszczyk | EclipseSource Austin | +1 860 839 2465
>> http://twitter.com/eclipsesource | http://twitter.com/caniszczyk
>
Previous Topic:pause in my swtbot's test
Next Topic:"No tests found"
Goto Forum:
  


Current Time: Wed Apr 24 15:40:43 GMT 2024

Powered by FUDForum. Page generated in 0.04287 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top