Posting screenshots to the log

Sometimes, it is necessary to place an image into the log; often, it may be a window screenshot, an image of a controls element, or even that of the whole of the screen. To this end, we use the Log.Picture method.

In this recipe we will consider different ways to place an image into the log.

How to do it...

The following steps should be performed to place an image to the log:

  1. First of all, we will create two image objects for the enabled window and the whole of the screen:
    var picWindow = Sys.Desktop.ActiveWindow().Picture();
    var picDesktop = Sys.Desktop.Picture();
  2. The image of the active window, now being stored in the picWindow variable, will be placed into the log, unchanged:
    Log.Picture(picWindow, "Active window");
  3. The image of the desktop is reduced by four times via the Stretch method, and then saved on to the file with the help of the SaveToFile method:
    picDesktop.Stretch(picDesktop.Size.Width/2, picDesktop.Size.Height/2);
    picDesktop.SaveToFile("c:\desktop.png");
  4. Now we go about creating a new variable of the Picture type, loading up an image into it from the earlier saved file, and then placing the same into the log:
    var pic = Utils.Picture;
    pic.LoadFromFile("c:\desktop.png");
    Log.Picture(pic, "Resized Desktop");
  5. As a result of function's execution, the log will contain the two images placed therein: that of the enabled window at the moment of test execution, and that of the reduced desktop copy.

How it works...

The Log.Picture method has one mandatory parameter that is, the image itself; the other parameters being optional.

Images of any of the onscreen objects (of a window, of a singular controls element, of the desktop) can be obtained via the Picture method. In our example, with the help of the method, we get the image of the desktop and that of the active window. Instead of the active window, we could use any variable that corresponds to a window or a controls element.

Any image can be saved onto the disk with the help of the SaveToFile method. The format of the saved image is determined by its extension (in our case, it is the PNG).

If it's necessary to obtain a variable containing the image from the file, we are supposed to create an empty variable placeholder with the help of the Utils.Picture property, and then with the help of the LoadFromFile method, we upload the image into it. In the future, one could handle the image as any other, received with the help of the Picture method.

Great-size images can be minified with the help of the Stretch method. The Stretch method uses two parameters: the new width and height of the image. With the help of the Size.Width and Size.Height properties, we could zoom in or out on the image in relation to its original size, without setting the dimensions explicitly.

There's more...

With the help of the Picture method, we could obtain not only the image of the whole window or a controls element, but just a part of it. For example, the following code gets an image of the upper left square of the desktop within the sizing of 50 x 50 pixels:

var picDesktop = Sys.Desktop.Picture(0,0, 50, 50);

The values of the parameters are as follows: coordinates of the left and right top corner, and its width and height.

Note

There is one important project setting which allows automatic posting images in case of error. To enable this option, right-click on the project name, navigate to Edit | Properties, click on Playback item from the list of options, and enable checkbox Post image on error.

Apart from changing the dimensions of the image, TestComplete allows for the execution of several, quite complicated imaging manipulations. For example, the comparison of the two images (the Compare method), searching for one image inside the other (the Find method), and so on. Click on the following link to get to know more about these possibilities:

http://support.smartbear.com/viewarticle/32131/

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.135.249