Recorder.AddTextToScript Method

Applies to TestComplete 15.69, last modified on November 13, 2024

Description

Use the Recorder.AddTextToScript method to add code or comments to the script being recorded by TestComplete. The code can be previously generated via the syntax objects, or it can be inserted as plain text.

It is recommended that you generate the code when recording in a language-independent way. For this purpose, you should first use the Syntax object to create a syntax element corresponding to the desired code and then call the Syntax.GenerateSource method.

It is also possible to insert the code as plain text, however, to do this you must know the correct syntax for all of the scripting languages supported by TestComplete - JavaScript, JScript, Python, VBScript, DelphiScript, C#Script and C++Script. Having defined a code snippet for each language, you can use the Syntax.CurrentLanguage property to determine which language is used in the user’s current project and record the appropriate code. For a sample, see the Examples section.

Declaration

Recorder.AddTextToScript(Text, AsComment)

Text [in]    Required    String    
AsComment [in]    Required    Boolean    
Result None

Applies To

The method is applied to the following object:

Parameters

The method has the following parameters:

Text

The code or comment text to be inserted into the script being recorded. When adding a comment (that is, if you set AsComment to True), Text should not start with the comment symbols (' or //).

AsComment

If this parameter is True, the Text will be commented out in the recorded script. Otherwise, if it is False, the Text will be inserted as the actual code, not a comment.

Result Value

None.

Remarks

The Recorder.AddTextToScript method can be used only in recording-time actions. It has no effect at design time.

The method has the following parameters:

Example

The following example illustrates how you can insert the Log.Message method call into the script being recorded:

JScript

function RecordLogMessageCall()
{
  // Create the Log.Message method call statement
  var oCall = Syntax.CreateInvoke();
  oCall.ClassValue = "Log";
  oCall.InvokeName = "Message";
  oCall.AddParameter("Hello, world!");
  oCall.IsProperty = false;

  // Generate the script code
  var strCode = Syntax.GenerateSource(oCall);

  Recorder.AddTextToScript(strCode);
}

VBScript

Sub RecordLogMessageCall
  Dim oCall, strCode

  ' Create the Log.Message method call statement
  Set oCall = Syntax.CreateInvoke
  oCall.ClassValue = "Log"
  oCall.InvokeName = "Message"
  oCall.AddParameter "Hello, world!"
  oCall.IsProperty = False

  ' Generate the script code
  strCode = Syntax.GenerateSource(oCall)

  Recorder.AddTextToScript strCode
End Sub

The example below does the same, but inserts the code as plain text instead of generating it via the Syntax object:

JScript

function RecordLogMessageCall()
{
  var str;

  switch (Syntax.CurrentLanguage)
  {
    case "JavaScript: case "JScript: case "Python": str = "Log.Message(\"Hello, world!\");"; break;
    case "VBScript" : str = "Log.Message \"Hello, world!\""; break;
    case "DelphiScript" : str = "Log.Message('Hello, world!');"; break;
    case "C#Script" : case "C++Script" : str = "Log[\"Message\"](\"Hello, world!\");"; break;
  }

  Recorder.AddTextToScript(str);
}

VBScript

Sub RecordLogMessageCall
  Dim str

  Select Case Syntax.CurrentLanguage
    Case "JavaScript", "JScript", "Python" str = "Log.Message(""Hello, world!"");"
    Case "VBScript" str = "Log.Message ""Hello, world!"""
    Case "DelphiScript" str = "Log.Message('Hello, world!');"
    Case "C#Script", "C++Script" str = "Log[""Message""](""Hello, world!"");"
  End Select

  Recorder.AddTextToScript str
End Sub

The code fragment below demonstrates how to record a comment:

JScript

Recorder.AddTextToScript("This is a comment", true);

VBScript

Recorder.AddTextToScript "This is a comment", True

See Also

AddSyntaxToScript Method
Syntax Object
GenerateSource Method
CurrentLanguage Property

Highlight search results