Working With Test Items in Scripts

Applies to TestComplete 14.20, last modified on September 11, 2019

TestComplete provides program objects that let you access properties of the project’s test items from scripts.

To obtain reference to the whole hierarchy of test items defined in your project, use the Project.TestItems property. It returns the TestItems object, which holds a list of the top-level test items. To refer child items of the top-level test items, use the TestItems.TestItem property with the item’s index specified.

You can also get a reference to the test item that is currently running via the Project.TestItems.Current property.

Each test item is represented by the TestItem object. For each test item’s property available on the Test Items page of the Project Editor, the TestItem object provides the corresponding property, which lets you obtain that value. For example, the TestItem.Timeout property returns the item’s timeout value.

The TestItem object also has additional properties, such as ItemCount, which returns the number of item’s child items and TestItem, which returns the reference to the child test item by its index. Using these properties, you can iterate through the child items of the current test item.

The following code example iterates through all test items defined in your project and posts their properties to the log:

JavaScript

function Main ()
{
  for (let i = 0; i < Project.TestItems.ItemCount; i++)
    LogTestItemInfo (Project.TestItems.TestItem(i));
}

function LogTestItemInfo (ti)
{
  var tiinfo = "Name: " + ti.Name + "\r\n" +
           "Description: " + ti.Description + "\r\n" +
           "Test: " + (!strictEqual(ti.ElementToBeRun, null) ? ti.ElementToBeRun.Caption : "not specified") + "\r\n" +
           "Enabled: " + ti.Enabled + "\r\n" +
           "On error: " + ti.StopOnError + "\r\n" +
           "Count: " + ti.Count + "\r\n" +
           "Timeout: " + ti.Timeout + "\r\n\r\n" +
           "Is currently running: " + ((ti.Iteration > 0) ? "yes" : "no");

  Log.AppendFolder (ti.Name, tiinfo);
  for (let i = 0; i < ti.ItemCount; i++)
    LogTestItemInfo (ti.TestItem(i));
  Log.PopLogFolder();
}

JScript

function Main ()
{
  for (var i = 0; i < Project.TestItems.ItemCount; i++)
    LogTestItemInfo (Project.TestItems.TestItem(i));
}

function LogTestItemInfo (ti)
{
  var tiinfo = "Name: " + ti.Name + "\r\n" +
           "Description: " + ti.Description + "\r\n" +
           "Test: " + ((ti.ElementToBeRun != null) ? ti.ElementToBeRun.Caption : "not specified") + "\r\n" +
           "Enabled: " + ti.Enabled + "\r\n" +
           "On error: " + ti.StopOnError + "\r\n" +
           "Count: " + ti.Count + "\r\n" +
           "Timeout: " + ti.Timeout + "\r\n\r\n" +
           "Is currently running: " + ((ti.Iteration > 0) ? "yes" : "no");

  Log.AppendFolder (ti.Name, tiinfo);
  for (var i = 0; i < ti.ItemCount; i++)
    LogTestItemInfo (ti.TestItem(i));
  Log.PopLogFolder();
}

Python

def Main():
  for i in range (0, Project.TestItems.ItemCount):
    LogTestItemInfo(Project.TestItems.TestItem[i])

def LogTestItemInfo(ti):
  tiinfo = "Name: " + ti.Name + "\r\n" + \
           "Description: " + ti.Description + "\r\n" + \
           "Test: " + (ti.ElementToBeRun.Caption if ti.ElementToBeRun != None else "not specified") + "\r\n" + \
           "On error: " + str(ti.StopOnError) + "\r\n" + \
           "Count: " + str(ti.Count) + "\r\n" + \
           "Timeout: " + str(ti.Timeout) + "\r\n\r\n" + \
           "Is currently running: " + ( "yes" if (ti.Iteration > 0) else "no")

  Log.AppendFolder(ti.Name, tiinfo)
  for i  in range (0, ti.ItemCount):
    LogTestItemInfo (ti.TestItem[i])
  Log.PopLogFolder()

VBScript

Sub Main
  Dim i

  For i = 0 To Project.TestItems.ItemCount-1
    LogTestItemInfo Project.TestItems.TestItem(i)
  Next
End Sub

Sub LogTestItemInfo (ti)
  Dim tiinfo, i

  tiinfo = "Name: " & ti.Name & vbCrLf & _
           "Description: " & ti.Description & vbCrLf & _
           "Test: "
  If Not ti.ElementToBeRun Is Nothing Then
    tiinfo = tiinfo & ti.ElementToBeRun.Caption & vbCrLf
  Else
    tiinfo = tiinfo & "not specified" & vbCrLf
  End If
  tiinfo = tiinfo & "Enabled: " & ti.Enabled & vbCrLf & _
           "On error: " & ti.StopOnError & vbCrLf & _
           "Count: " & ti.Count & vbCrLf & _
           "Timeout: " & ti.Timeout & vbCrLf & vbCrLf + _
           "Is currently running: "
  If ti.Iteration > 0 Then
    tiinfo = tiinfo & "yes"
  Else
    tiinfo = tiinfo & "no"
  End If

  Call Log.AppendFolder (ti.Name, tiinfo)
  For i=0 To ti.ItemCount-1
    LogTestItemInfo (ti.TestItem(i))
  Next
  Log.PopLogFolder
End Sub

DelphiScript

procedure LogTestItemInfo (ti);
var tiinfo, i;
begin
  tiinfo := 'Name: ' + ti.Name + #13#10 +
            'Description: ' + ti.Description + #13#10 +
            'Test: ';
  if (ti.ElementToBeRun <> nil) then
    tiinfo := tiinfo + ti.ElementToBeRun.Caption + #13#10
  else
    tiinfo := tiinfo + 'not specified' + #13#10;
  tiinfo := tiinfo + 'Enabled: ' + aqConvert.VarToStr (ti.Enabled) + #13#10 +
            'On error: ' + aqConvert.VarToStr (ti.StopOnError) + #13#10 +
            'Count: ' + aqConvert.VarToStr (ti.Count) + #13#10 +
            'Timeout: ' + aqConvert.VarToStr (ti.Timeout) + #13#10#13#10 +
            'Is currently running: ';
  if (ti.Iteration > 0) then
    tiinfo := tiinfo + 'yes'
  else
    tiinfo := tiinfo + 'no';

  Log.AppendFolder (ti.Name, tiinfo);
  for i:=0 to ti.ItemCount-1 do
    LogTestItemInfo (ti.TestItem[i]);
  Log.PopLogFolder;
end;

procedure Main;
var i;
begin
  for i:=0 to Project.TestItems.ItemCount-1 do
    LogTestItemInfo (Project.TestItems.TestItem[i]);
end;

C++Script, C#Script

function Main ()
{
  for (var i = 0; i < Project["TestItems"]["ItemCount"]; i++)
    LogTestItemInfo (Project["TestItems"]["TestItem"](i));
}

function LogTestItemInfo (ti)
{
  var tiinfo = "Name: " + ti["Name"] + "\r\n" +
           "Description: " + ti["Description"] + "\r\n" +
           "Test: " + ((ti["ElementToBeRun"] != null) ? ti["ElementToBeRun"]["Caption"] : "not specified") + "\r\n" +
           "Enabled: " + ti["Enabled"] + "\r\n" +
           "On error: " + ti["StopOnError"] + "\r\n" +
           "Count: " + ti["Count"] + "\r\n" +
           "Timeout: " + ti["Timeout"] + "\r\n\r\n" +
           "Is currently running: " + ((ti["Iteration"] > 0) ? "yes" : "no");

  Log["AppendFolder"] (ti["Name"], tiinfo);
  for (var i = 0; i < ti["ItemCount"]; i++)
    LogTestItemInfo (ti["TestItem"](i));
  Log["PopLogFolder"]();
}

See Also

Tests, Test Items, and Test Cases
Test Items Page (Project Editor)
Project.TestItems Property
Projects in TestComplete

Highlight search results