4. Creating the Record-Time Action's Code

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

Now we can create the actions. We will start with the record-time action as its code is easier.

To create the record-time action, you should perform the following:

  • Create the script code that will be executed when a user selects an item from the Recording toolbar.

  • Add information about the action to the description.xml file.

This topic describes the creation of the script code. In the next topic we will describe how to update the description file.

As you may remember, the record-time action should obtain the clipboard value, generate the comparison code, and automatically insert it into the recorded script. Below is the code that performs these tasks. Open the clcCode.js unit for editing and enter this code:

JScript

[clcCode.js]

// Generate the checkpoint code
function CreateSyntax()
{
  var clipboard;
  
  // Obtain the clipboard contents
  clipboard = Sys.Clipboard;

  // Check the clipboard contents format
  if (aqObject.GetVarType(clipboard) != 8)
  {
      MessageDlg("Unable to create the clipboard checkpoint, because the clipboard does not contain text.", mtError, mbOK)
      return null;
  }

  // Generate the Sys.Clipboard call
  var call;
  call = Syntax.CreateInvoke();
  call.ClassValue = Sys;
  call.InvokeName = "Clipboard";
  call.IsProperty = true;

  // Generate a comparison operator
  var condition;
  condition = Syntax.CreateCondition();
  condition.OperatorType = condition.otInequality;
  condition.Left = call;
  condition.Right = clipboard;

  // Generate the Log.Error call
  call = Syntax.CreateInvoke();
  call.ClassValue = "Log";
  call.InvokeName = "Error";
  call.AddParameter("Clipboard checkpoint failed");
  call.IsProperty = false;

  // Generate the if statement
  var ifSyntax;
  ifSyntax = Syntax.CreateIf();
  ifSyntax.Condition = condition;
  ifSyntax.TrueSyntax = call;

  // Return the result
  return ifSyntax;
}

// Record an action
function RecordExecute()
{
  var syntax;

  // Generate comparison code
  syntax = CreateSyntax();
  if (syntax == null)
    return;

  // Insert the generated code into the recorded script
  Recorder.AddSyntaxToScript(syntax);
}

The code snippet above has two functions: RecordExecute and CreateSyntax. The RecordExecute function is the routine that will be called when a user selects our record-time action from the Recording toolbar. As you can see, the routine calls the helper CreateSyntax function to generate the checkpoint code, and then calls the AddSyntaxToScript method of the Recorder object to insert the generated code into the recorded script.

CreateSyntax is a helper function that generates the checkpoint code. First, it obtains the current data from the clipboard and checks the data format. Then, it uses methods of the Syntax object to generate objects that correspond to the syntax statements and operators. The script code that is generated this way is compatible with any scripting language of the current test project. This functionality makes the script extension’s code independent from the scripting language of the user’s test projects. For detailed information on generating script code and inserting it into the recorded script, see Generating Script Code.

In the next steps, we will create an icon for our record-time action and then add information about the action to the description.xml file.

Prev     Next

See Also

Creating Custom Actions
Creating Actions - Basic Concepts
Writing Action Code
Generating Script Code

Highlight search results