Generating Comments

Applies to TestComplete 15.62, last modified on March 19, 2024

Custom actionscreated with script extensions can generate code that contains comments. Unlike other statements, which are created via syntax elements, comment statements are generated directly based on the comment text. Note, however, that comments are generated differently during the test recording and at design time.

To generate a comment during test recording

To insert a comment into the script being recorded, use the Recorder.AddTextToScript method with the comment text specified in the first parameter and the second parameter set to True. For example:

JScript

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

VBScript

Recorder.AddTextToString "This is a comment", True

The recorded comment is as follows:

JavaScript, JScript

// This is a comment

Python

# This is a comment

VBScript

' This is a comment

DelphiScript

// This is a comment

C++Script, C#Script

// This is a comment

To generate a comment at design time

To create code with comments, write a custom routine that will turn text into a comment statement that is valid for the language of the current project.

The example below demonstrates how you can do this. It gets the scripting language the current project uses and transforms the specified text into the corresponding comment statement. The routine supports both single-line and multi-line comments:

JScript

function ShowComment()
{
  var str = GetComment("This is a comment");
  aqDlg.ShowMessage(str);
}

function GetComment(Text)
{
  var prefix;
  
  switch (Syntax.CurrentLanguage)
  {
    case "VBScript": prefix = "' "; break;
    case "Python": prefix = "# "; break;
    default: prefix = "// ";
  }
  var tmp = Text.split("\r\n");
  for (var i = 0; i < tmp.length; i++)
    tmp[i] = prefix + tmp[i];
  return tmp.join("\r\n");
}

VBScript

Sub ShowComment
  Dim str

  str = GetComment("This is a comment")
  aqDlg.ShowMessage str
End Sub

Function GetComment(Text)
  Dim prefix, tmp, i

  Select Case (Syntax.CurrentLanguage)
    Case "VBScript" prefix = "' "
    Case "Python" prefix = "# "
    Case Else prefix = "// "
  End Select

  tmp = Split(Text, vbNewLine)
  For i = 0 to UBound(tmp)
    tmp(i) = prefix & tmp(i)
  Next

  GetComment = Join(tmp, vbNewLine)
End Function

The code generated in this example is as follows:

JavaScript, JScript

// This is a comment

Python

# This is a comment

VBScript

' This is a comment

DelphiScript

// This is a comment

C++Script, C#Script

// This is a comment

See Also

Generating Script Code
AddTextToScript Method

Highlight search results