5. Creating Initialization Code

Applies to TestComplete 14.72, last modified on April 22, 2021

Custom keyword test operations created with script extensions may have initialization routines that serve as operation constructors. The initialization routine is executed when TestComplete creates a new instance of the operation and is typically used to specify default values of operation parameters and fields. Creation of the initialization routine includes the following steps:

  1. Writing the routine’s code.

  2. Subscribing to the operation’s OnCreate event in the description.xml file and specifying the routine as the event handler.

On the previous step, we added a number of fields to our custom operation. Let’s create the initialization routine that will assign default values to these fields. We will name this routine LogAttr_OnCreate.

Start with writing the routine’s code:

  • Open the laCode.js unit in the Code Editor.

  • Add the following code at the beginning of the script:

    JScript

    [laCode.js]

    // This routine is executed when a new operation instance is created.
    // It initializes the operation's fields.
    function LogAttr_OnCreate(Data)
    {
      Data.Description = "Defines the font and color settings for test log items.";
      Data.FontColor = GetColorByName("Black");
      Data.BackColor = GetColorByName("White");
      Data.Bold = false;
      Data.Italic = false;
      Data.Underline = false;
      Data.Strikeout = false;
      Data.PlainTextRemarks = true;
    }

    // Available color names and RGB values
    var ColorNames = ["Black", "Maroon", "Green", "Olive", "Navy", "Purple", "Teal", "Gray",
                      "Silver", "Red", "Lime", "Yellow", "Blue", "Fuchsia", "Aqua", "White",
                      "Money Green", "Sky Blue", "Cream", "Medium Gray", "Orange", "Brown"];

    var ColorValues = [0x000000, 0x000080, 0x008000, 0x008080, 0x800000, 0x800080, 0x808000, 0x808080,
                       0xC0C0C0, 0x0000FF, 0x00FF00, 0x00FFFF, 0xFF0000, 0xFF00FF, 0xFFFF00, 0xFFFFFF,
                       0xC0DCC0, 0xF0CAA6, 0xF0FBFF, 0xA4A0A0, 0x00A5FF, 0x004B96];

    // Returns the RGB value by the color name
    function GetColorByName(ColorName)
    {
      for (var i in ColorNames)
        if (ColorNames[i] == ColorName)
          return ColorValues[i];

      return ColorValues[0]; // Black
    }

    // Returns the color name by the RGB value
    function GetColorName(Color)
    {
      for (var i in ColorValues)
        if (ColorValues[i] == Color)
          return ColorNames[i];

      return ColorNames[0];
    }

  • Save the changes.

As you can see from this code, the initialization routine, LogAttr_OnCreate, has one parameter, Data. This is a special object that provides access to the operation’s fields. It contains properties that correspond to the operation’s fields and whose names coincide with the field names. The LogAttr_OnCreate routine refers to the operation’s fields via the Data object and thus assigns the initial values to the fields. Later on you will notice that all operation-related script routines have the Data argument; its presence in these routines is a must.

Two of the operation fields, namely, FontColor and BackColor, hold color values. These are 4-byte integer numbers (usually, in a hexadecimal format), where the low 3 bytes represent intensities for blue, green and red components respectively. For example, the value for Sky Blue is 0xF0CAA6, where F0 (hexadecimal value of 240) is the blue component, CA (hexadecimal value of 202) is the green component and A6 (hexadecimal value of 166) is the red component. However, it is much more convenient for the users to deal with color names rather than color values. So, we have defined two helper arrays -- ColorNames and ColorValues -- whose elements specify common color names and the appropriate color values in the respective order. We have also created helper GetColorByName and GetColorName functions that convert between color names and values. The initialization routine uses one of these helper routines, GetColorByName, to get values of default colors used by test log entries.

Note that you can also add custom color definitions to the ColorNames and ColorValues arrays. Just make sure that the color name and value in these arrays are at the same position. For a list of colors, see:

Color Table and HTML Color Table articles on MSDN

List of colors provided by Wikipedia

Now let’s add information about the initialization routine to the description file:

  • Open the C:\My Extension Files\LogAttrExtension\description.xml file in your test or XML editor.

  • Add the following text to the file:

    XML

    [description.xml]

    <?xml version = "1.0" encoding = "UTF-8"?>
    <ScriptExtensionGroup>
      <Category Name = "Keyword Test Operations">
        <ScriptExtension Name = "Log Attributes Keyword Test Operation" Author = "SmartBear Software" Version = "1.0 test" HomePage = "smartbear.com">
          <Description>Provides the Log Attributes keyword test operation.</Description>
          <Script Name = "laCode.js">

            <KDTOperation Name = "Log Attributes" Category = "Logging">
              <Data>
                <!-- General properties -->
                <Field Name = "Description" />
                <!-- Log Attributes object properties -->
                <Field Name = "FontColor" />
                <Field Name = "BackColor" />
                <Field Name = "Bold" />
                <Field Name = "Italic" />
                <Field Name = "Underline" />
                <Field Name = "Strikeout" />
                <Field Name = "PlainTextRemarks" />
              </Data>
              <Events>
                <Event Name="OnCreate" Routine="LogAttr_OnCreate"/>
              </Events>

            </KDTOperation>

          </Script>
        </ScriptExtension>
      </Category>
    </ScriptExtensionGroup>

  • Save the changes.

You may notice that we have added a child Events element to the KDTOperation element and one child Event element to the Events element. The Event elements are used to subscribe to various events that occur in the operation and to define script routines to be used as the event handlers.

Let’s take a closer look at the Event element:

XML

[description.xml]

...
<Events>
  <Event Name="OnCreate" Routine="LogAttr_OnCreate"/>
</Events>
...

The element’s Name attribute specifies the name of the event we want to handle -- OnCreate. This event occurs when an operation instance is created. The Routine attribute holds the name of operation’s initialization routine, which will be used as the event handler. This is the LogAttr_OnCreate routine that we have created earlier in this topic.

For details on elements and attributes that are used in the description files, see topics of the Script Extension Files section.

Prev     Next

See Also

Script Extensions
Creating Operation Initialization Routine

Highlight search results