Writing Action Code

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

The code of an action is simply a script routine that is executed whenever a user selects this action on the TestComplete toolbar. This routine must meet the following requirements:

  • It must not have parameters.

  • It must be a procedure, that is, it must not return a value.

To learn how you can implement some of the common tasks used in testing, see the following topics:

The following sample routine demonstrates the code of a record-time action that records a clipboard checkpoint.

The code to be recorded by the action:

JavaScript, JScript

if (Sys.Clipboard != "Clipboard text")
  Log.Error("Clipboard checkpoint failed.");

Python

if (Sys.Clipboard != "Clipboard text"):
  Log.Error("Clipboard checkpoint failed.")

VBScript

If (Sys.Clipboard <> "Clipboard text") Then
  Call Log.Error("Clipboard checkpoint failed.")
End If

DelphiScript

if Sys.Clipboard <> 'Clipboard text' then
  Log.Error('Clipboard checkpoint failed.');

C++Script, C#Script

if (Sys["Clipboard"] != "Clipboard text")
  Log["Error"]("Clipboard checkpoint failed.");

The action’s code:

JScript

function RecordClipboardCheckpoint()
{
  var oSysClipboard = Syntax.CreateInvoke();
  oSysClipboard.ClassValue = "Sys";
  oSysClipboard.InvokeName = "Clipboard";
  oSysClipboard.IsProperty = true;

  var oCondition = Syntax.CreateCondition();
  oCondition.OperatorType = oCondition.otInequality;
  oCondition.Left = oSysClipboard;
  oCondition.Right = Sys.Clipboard;

  var oLogError = Syntax.CreateInvoke();
  oLogError.ClassValue = "Log";
  oLogError.InvokeName = "Error";
  oLogError.AddParameter("Clipboard checkpoint failed.");
  oLogError.IsProperty = false;

  var oIf = Syntax.CreateIf();
  oIf.Condition = oCondition;
  oIf.TrueSyntax = oLogError;

  Recorder.AddSyntaxToScript(oIf);
}

VBScript

Sub RecordClipboardCheckpoint
  Dim oSysClipboard, oCondition, oLogError, oIf

  Set oSysClipboard = Syntax.CreateInvoke
  oSysClipboard.ClassValue = "Sys"
  oSysClipboard.InvokeName = "Clipboard"
  oSysClipboard.IsProperty = True

  Set oCondition = Syntax.CreateCondition
  oCondition.OperatorType = oCondition.otInequality
  oCondition.Left = oSysClipboard
  oCondition.Right = Sys.Clipboard

  Set oLogError = Syntax.CreateInvoke
  oLogError.ClassValue = "Log"
  oLogError.InvokeName = "Error"
  oLogError.AddParameter "Clipboard checkpoint failed."
  oLogError.IsProperty = False

  Set oIf = Syntax.CreateIf
  oIf.Condition = oCondition
  oIf.TrueSyntax = oLogError

  Recorder.AddSyntaxToScript oIf
End Sub

To create record-time and design-time actions of the same functionality, you can use the same routine for them, unless this routine uses scripting functionality specific for test recording or test design. For example, the Recorder and Stores objects can only be used in record-time actions. As a workaround, you can create a helper routine with a common functionality, and call this routine from the action’s routines.

The following example, which is based on the previous one, demonstrates how this can be done. It contains three routines: RecordClipboardCheckpoint and CreateClipboardCheckpoint are routines of record-time and design-time actions respectively; CreateCheckpointSyntax is a helper function that generates the script code as a syntax object. First, both the RecordClipboardCheckpoint and CreateClipboardCheckpoint routines call the CreateCheckpointSyntax function to get the syntax of the checkpoint code; then each routine utilizes this syntax in its own way.

JScript

// This routine is called by a record-time action
function RecordClipboardCheckpoint()
{
  var oSyntax = CreateCheckpointSyntax();
  Recorder.AddSyntaxToScript(oSyntax);
}

// This routine is called by a design-time action
function CreateClipboardCheckpoint()
{
  var oSyntax = CreateCheckpointSyntax();
  var strCode = Syntax.GenerateSource(oSyntax);
  Sys.Clipboard = strCode;
}

// This helper function returns the checkpoint code as a syntax tree
function CreateCheckpointSyntax()
{
  var oSysClipboard = Syntax.CreateInvoke();
  oSysClipboard.ClassValue = "Sys";
  oSysClipboard.InvokeName = "Clipboard";
  oSysClipboard.IsProperty = true;

  var oCondition = Syntax.CreateCondition();
  oCondition.OperatorType = oCondition.otInequality;
  oCondition.Left = oSysClipboard;
  oCondition.Right = Sys.Clipboard;

  var oLogError = Syntax.CreateInvoke();
  oLogError.ClassValue = "Log";
  oLogError.InvokeName = "Error";
  oLogError.AddParameter("Clipboard checkpoint failed.");
  oLogError.IsProperty = false;

  var oIf = Syntax.CreateIf();
  oIf.Condition = oCondition;
  oIf.TrueSyntax = oLogError;

  return oIf;
}

VBScript

' This routine is called by a record-time action
Sub RecordClipboardCheckpoint
  Dim oSyntax

  Set oSyntax = CreateCheckpointSyntax
  Recorder.AddSyntaxToScript oSyntax
End Sub

' This routine is called by a design-time action
Sub CreateClipboardCheckpoint
  Dim oSyntax, strCode

  Set oSyntax = CreateCheckpointSyntax
  strCode = Syntax.GenerateSource(oSyntax)
  Sys.Clipboard = strCode
End Sub

' This helper function returns the checkpoint code as a syntax tree
Function CreateCheckpointSyntax
  Dim oSysClipboard, oCondition, oLogError, oIf

  Set oSysClipboard = Syntax.CreateInvoke
  oSysClipboard.ClassValue = "Sys"
  oSysClipboard.InvokeName = "Clipboard"
  oSysClipboard.IsProperty = True

  Set oCondition = Syntax.CreateCondition
  oCondition.OperatorType = oCondition.otInequality
  oCondition.Left = oSysClipboard
  oCondition.Right = Sys.Clipboard

  Set oLogError = Syntax.CreateInvoke
  oLogError.ClassValue = "Log"
  oLogError.InvokeName = "Error"
  oLogError.AddParameter "Clipboard checkpoint failed."
  oLogError.IsProperty = False

  Set oIf = Syntax.CreateIf
  oIf.Condition = oCondition
  oIf.TrueSyntax = oLogError

  Set CreateCheckpointSyntax = oIf
End Function

See Also

Script Extensions
Creating Script Extensions
Creating Custom Actions

Highlight search results