9. Creating Operation Setup Code

Applies to TestComplete 15.70, last modified on December 17, 2024

On the previous step, we have created a form that allows the users to modify the operation’s fields. Now we need to write the code that displays this form on screen and processes the inputted values. We will place this code in a routine that will be used as the operation’s setup routine. This is a special routine that is executed when the user adds the operation to the test or double-clicks the operation in the test. It is used to specify the basic way of modifying the operation’s fields.

Creation of the setup routine consists of two steps:

  • Writing the routine that performs the desired tasks, for example, displays a form, checks the dialog result and saves the values inputted by the user.

  • Subscribing to the operation’s OnSetup event in the description file and specifying the routine as the event handler.

Let’s write the routine code first. We will name this routine LogAttr_OnSetup. Open the laCode unit in the TestComplete Code Editor and append the following code to it:

JScript

[laCode.js]

// This routine is executed when the operation is being edited.
// It displays the Operation Properties dialog.
function LogAttr_OnSetup(Data)
{
  // Customize the Operation Properties dialog
  var frm = UserForms.laEditForm;

  if (frm.cbFontColor.Properties.Items.Count == 0)
    AddColors(frm.cbFontColor);
  if (frm.cbBackColor.Properties.Items.Count == 0)
    AddColors(frm.cbBackColor);

  frm.cbFontColor.Text = GetColorName(Data.FontColor);
  frm.cbBackColor.Text = GetColorName(Data.BackColor);
  frm.chkBold.Checked = Data.Bold;
  frm.chkItalic.Checked = Data.Italic;
  frm.chkUnderline.Checked = Data.Underline;
  frm.chkStrikeout.Checked = Data.Strikeout;        
  frm.cbRemarksFormat.ItemIndex = (Data.PlainTextRemarks ? 1 : 0);

  // Display the Operation Properties dialog and check the results
  if (frm.ShowModal() != mrOk)
    return false;    // No changes were made

  // Save the changes
  Data.FontColor = GetColorByName(frm.cbFontColor.Text);
  Data.BackColor = GetColorByName(frm.cbBackColor.Text);
  Data.Bold = frm.chkBold.Checked;
  Data.Italic = frm.chkItalic.Checked;
  Data.Underline = frm.chkUnderline.Checked;
  Data.Strikeout = frm.chkStrikeout.Checked;    
  Data.PlainTextRemarks = (frm.cbRemarksFormat.ItemIndex == 1);

  return true;
}

// Populates the specified combo box with predefined color names
function AddColors(Combobox)
{
  var Items = Combobox.Properties.Items;
  Items.Clear();
  for (var i in ColorNames)
    Items.Add(ColorNames[i]);
}

The routine performs the following actions:

  • Obtains a reference to the laEditForm form included in the script extension.

  • Checks if the form is displayed for the first time during the current TestComplete session and, if so, fills the lists of font and background colors.

  • Adjusts the state of the form’s controls according to the operation’s field values.

  • Displays the form and checks the dialog result. If the user closed the form using the Cancel button, exit the routine.

  • Copies values of the form’s controls to the operation’s fields.

The above code also contains a helper AddColors routine that adds items of the global ColorNames array to the specified TcxComboBox control on a form. The GetColorName and GetColorByName routines and the ColorNames array referred in the code were defined on one of the previous steps.

Now we need to add information about the setup routine to the description file of our script extension:

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

  • Add the following code to it:

    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" Icon = "la-icon.bmp">
              <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"/>
                <Event Name="OnExecute" Routine="LogAttr_OnExecute"/>
                <Event Name="OnSetup" Routine="LogAttr_OnSetup"/>
              </Events>
            </KDTOperation>

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

  • Save the changes.

We have added one more Event element that specifies the newly created LogAttr_OnSetup routine as a handler for the OnSetup event of our custom operation. This event occurs when the user adds the operation to the test or double-clicks it in the test. This way, the LogAttr_OnSetup routine will be executed each time one of these events happen.

Prev     Next

See Also

Script Extensions
Creating the Operation Setup Routine

Highlight search results