6 min read

(For more resources related to this topic, see here.)

Posting messages to the log

TestComplete allows committing various types of messages to the log: ordinary messages, warnings, logs, and so on.

In this section, we will consider examples of how to use these messages.

Getting ready

Create a file with the name myfile.txt in the root directory of C:.

How to do it…

In order to see examples of all the message types in the log, the following steps should be performed:

  1. Create and launch the following function:

    function testMessages() { Log.Event("An event", "Event additional Info"); Log.Message("A message", "Message additional Info"); Log.Warning("A warning", "Warning additional Info"); Log.Error("An error", "Error additional Info"); Log.File("C:\somefile.txt", "A file posted to the log"); Log.Link("C:\somefile.txt", "A link to a file"); Log.Link("http://smartbear.com/", "HTTP link"); Log.Link("ftp://smartbear.com/", "FTP link"); }

    In the result, we will get the following screenshot of the log

How it works…

In the given example, we have used four different types of messages. They are as follows:

  • Log.Event: This message is an event which occurs when TestComplete interacts with a tested application. Usually, messages of this type are placed into the log at the point of text input or mouse-clicks; however, we can also place custom-made events into the log.
  • Log.Message: This message is an ordinary message that is usually used for prompting a user concerning current actions that are being executed by the script (usually, of a higher level than that of the events; for example, creation of a user, searching for a record, and so on).
  • Log.Warning: This message is a non-critical error. It is used in case the results of the check are different from those expected; nonetheless, execution of the script can carry on.
  • Log.Error: This message is a critical error usually used when an error is a critical one, making any further execution of the test would be futile

These four types of message are based on several parameters. The first of them is a string that we observe in the log itself; the second one contains additional information which can be seen in the Additional Info tab, if the message has been clicked on. The second parameter is optional and can be omitted as well as all other parameters.

There are two more types of messages:

  • Log.File: This message copies the assigned file into the file with the log, and places a reference-pointer to it. Meanwhile, TestComplete renames the file to avoid naming conflicts, leaving only the original extension intact.
  • Log.Link: This message places a link to the web page or a file, without making a copy of the file itself in the folder with the log. On clicking on the link, the file will open with the help of the associated program or a link in the browser.

These two types of message accept the link as the first parameter, and then the message parameters, and those pertaining to the additional information (as the previous four). Only the first parameter is mandatory.

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 section 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.

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:


Subscribe to the weekly Packt Hub newsletter

* indicates required


Please enter your comment!
Please enter your name here