Assessing the number of errors in the log

TestComplete allows you to retrieve the number of errors for the current test item (with the help of the Log.ErrCount property); however, there is no way of finding out the total number of errors in all the executed tests. Such a possibility can be useful only if a certain predefined number of errors is treated as critical, upon reaching which test execution should be stopped altogether.

How to do it...

First of all, we will add two new variables on the level of Project Suite as follows:

  1. Right-click on the name of the Project Suite (in the Project Workspace toolbar, to the left) and navigate to Edit | Variables.
  2. On the Temporary Variables list, right-click on the New Item menu item.
  3. In the Name field, input the name of the ErrorsTotal variable, into the field Type select Integer, and set the Default Value field to 0.

    This variable will be a counter of the errors.

  4. Similarly, add the variable with the name of ErrorsMax. As Default Value, set the number of errors which should signal stopping test execution (in our example, those are equal to 3).

    In the result, we will have two new variables created, as shown in the following screenshot:

    How to do it...
  5. Now we will create a handler for the OnLogError event, which will increase the counter of the errors. For this purpose, perform the following steps:
    1. Add the Events element to the project, if it is still missing (right-click on the name of the project and navigate to Add | New Item | Events).
    2. Navigate to Events | General Events on the element. In the result, the events panel will be opened.
    3. In the right section of the panel (the Events to Handle column), unfold the element of General Events and highlight the event of OnLogError.
    4. Click on the button New inside the OnLogError element.
    5. In the opened window New Event Handler, select the module in which you will store the event handler, and then click on the OK button.
    6. In the result, we will have an empty function created with the name of GeneralEvents_OnLogError.
    7. Change the function in the following manner:
      function GeneralEvents_OnLogError(Sender, LogParams)
      {
        ProjectSuite.Variables.ErrorsTotal += 1;
        if(ProjectSuite.Variables.ErrorsTotal > ProjectSuite.Variables.ErrorsMax)
        {
          Runner.Stop();
        }
      }
    8. Now we will write a simple function which will create 10 error messages in the log:
      function testErrorsCount()
      {
        for(var i = 0; i < 10; i++)
        {
          Log.Error("Error #" + i);
        }
      }
    9. If we launch the testErrorsCount function now we would see that upon the fourth emergent error, test execution would be stopped, since the number of arisen errors exceeded the preset value of the ErrorsMax variable.

How it works...

In the OnLogError events handler, we increase the number of emerging errors each time the error is generated in the log (no matter how: either with the help of the method Log.Error or via TestComplete in itself, the event will be processed all the time).

When the number of errors exceed the preset threshold, we stop execution of the tests with the help of the Runner.Stop method.

To the variables on the level of Project Suite there exists access from any project. Therefore it is unimportant how exactly tests are launched: from the Project Suite, or just a project or several separate functions. In any case, the variable will be updated each time an error occurs.

It's worthwhile to note, if you have several projects running in Project Suite, each one of them should contain some sort of handler. To this end, it is much easier to create such a handler in one project and in the other projects add an existing module (by right-clicking on the Script element, thus opting for the Existing Item menu item in the Add menu). This allows us to avoid code duplication.

There's more...

In the same manner, one could trace the number of other messages (Event, Warning, File, and so on), since for each of them there exists a corresponding property, containing their number in the current test item (Log.EvnCount, Log.WrnCount, Log.FileCount, and so on).

See also

  • To know more about event handlers in TestComplete, refer to the Creating even handlers recipe in Chapter 12, Events Handling
..................Content has been hidden....................

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