How can I take a screenshot of application if any test fails ?

Oct 15, 2012 at 10:13 AM

I am trying to automate testing of WPF application.

How can I take a screenshot of application if any test fails ?

can anyone post a example

Oct 15, 2012 at 11:27 AM
Edited Oct 15, 2012 at 11:28 AM

Hi 

You might need to write one yourself like I have. I've created a C# service which sits in the background until such a error is triggered (depending on your test).

The code below will grab the screen and you can amend this for areas of the screen you are interested in.

 

protected static void CaptureScreenShot(System.Drawing.Point SourcePoint, System.Drawing.Point DestinationPoint, Rectangle SelectionRectangle, string filePath)
{
	using (Bitmap bitmap = new Bitmap(SelectionRectangle.Width, SelectionRectangle.Height))
        {
        	using (Graphics gdi = Graphics.FromImage(bitmap))
                {
                	try
                    	{
                        	gdi.CopyFromScreen(SourcePoint, DestinationPoint, SelectionRectangle.Size);
                                                bitmap.Save(filePath, ImageFormat.Jpeg);
	                        gdi.Dispose();
        	         }
                    	catch (Exception ex)
                    	{
                        
			}
                }
         }
          
 }
Oct 19, 2012 at 12:25 PM

Hi,

I am trying to use this code but getting the exception -

 System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.

I am trying to call the code as follows -

Rectangle r = new Rectangle();
              Size size = new Size();
              size.Height = 100;
              size.Width = 100;
              r.Size = size;
              Point psource = new Point();
              psource.X = 100;
              psource.Y = 100;
              Point pdest = new Point();
              pdest.X = 100;
              pdest.Y = 100;
              CaptureScreenShot(psource, pdest,  r, "C:\\TEMP");

Can you please provide be the code to call this function so that I can pass the correct parameters to it. Thanks.

Oct 19, 2012 at 12:29 PM

Hi

 

Try this

System.Drawing.Point source = System.Drawing.Point.Empty;           

System.Drawing.Point destination = System.Drawing.Point.Empty;           

Rectangle selection = Rectangle.Empty;           

string fileName = screenShotFilename;

Rect rect = AutomationElement.RootElement.Current.BoundingRectangle;
selection.Height = Convert.ToInt32(rect.Size.Height);           

selection.Width = Convert.ToInt32(rect.Size.Width);           

source.X = 0;           

source.Y = 0;           

destination.X = Convert.ToInt32(rect.TopRight.Y);           

destination.Y = Convert.ToInt32(rect.BottomLeft.X);           

CaptureScreenShot(source, destination, selection, fileName);

 

Let me know if you are still having difficulties

 

Ant

Oct 19, 2012 at 12:51 PM

Hi,

Thanks a lot for the reply..I am getting an error in Rect. What is the namespace and dll you have used for it.

Thanks a lot..!!

Oct 19, 2012 at 1:06 PM
Edited Oct 19, 2012 at 1:30 PM

Awesome..Thanks a lot CygnusAlpha It works g8..I am posting the full code on your behalf-

DLL  required - System.Drawing and WindowsBase

using System.Drawing;
using System.Drawing.Imaging;

public static void Main ()

{

System.Drawing.Point source = System.Drawing.Point.Empty;

              System.Drawing.Point destination = System.Drawing.Point.Empty;

              Rectangle selection = Rectangle.Empty;

              string fileName = "snapshot.jpg";

           System.Windows.Rect  rect = AutomationElement.RootElement.Current.BoundingRectangle;
              selection.Height = Convert.ToInt32(rect.Size.Height);

              selection.Width = Convert.ToInt32(rect.Size.Width);

              source.X = 0;

              source.Y = 0;

              destination.X = Convert.ToInt32(rect.TopRight.Y);

              destination.Y = Convert.ToInt32(rect.BottomLeft.X);          
              CaptureScreenShot(source, destination, selection, fileName);

}

protected static void CaptureScreenShot(System.Drawing.Point SourcePoint, System.Drawing.Point DestinationPoint, Rectangle SelectionRectangle, string filename)
      {
          using (Bitmap bitmap = new Bitmap(SelectionRectangle.Width, SelectionRectangle.Height))
          {
              using (Graphics gdi = Graphics.FromImage(bitmap))
              {
                  try
                  {
                      gdi.CopyFromScreen(SourcePoint, DestinationPoint, SelectionRectangle.Size);
                      bitmap.Save(filename, ImageFormat.Jpeg);
                      gdi.Dispose();
                  }
                  catch (Exception ex)
                  {

                
                  }
              }
          }

      }

Thanks a Lot..!!!

Oct 19, 2012 at 1:09 PM

No worries. 

Apologies for not posting everything earlier. Glad you got it to work.

Oct 19, 2012 at 1:14 PM
Edited Oct 19, 2012 at 1:18 PM

I have one more doubt. Hope I am not causing much trouble to you.

Currently the file gets saved in the project folder. Do you have any idea that how can it be stored to any other location.

Thanks.

Oct 19, 2012 at 1:18 PM

hi akansh

Instead of just giving the file name pass the Filename with full path where u want to save. I think it will work. Though i have not tried it.

 

Regards,

Anand

Oct 19, 2012 at 1:20 PM

example

string fileName = "D:\snapshot.jpg"; which will save it to D: folder

Oct 19, 2012 at 1:20 PM

Filename can be a full path and name to any location although I've only tried it locally as opposed to a network location.

e.g

bitmap.Save("C:\Temp\Foo\Temp\screenshot.jpg", ImageFormat.Jpeg);

 


Oct 19, 2012 at 1:29 PM

Hi ananddave123,CygnusAlpha

I tried as per your examples and it works perfectly.

Thanks.