Enumerator

Applies to TestComplete 15.46, last modified on January 09, 2023

TestComplete provides the Enumerator object that helps you iterate through collections. This object works in any scripting language. It is an analog of the for...of statement in JavaScript, of the Enumerator object in JScript, Python, C#Script and C++Script, of the For Each statement in VBScript and of the for each statement in Delphi 2005 and later. To obtain an enumerator for a collection, use the Utils.Enumerator method.

The enumerator is initially positioned at the first item in the collection. The MoveFirst method also resets the enumerator to this position. The Item method returns the current item in the collection and the MoveNext method advances the enumerator to the next element. Once the enumerator is positioned at the end of the collection, the AtEnd method returns True. To bring the enumerator back to the first element, use the MoveFirst method.

The following code shows the Enumerator object usage. It posts a list of currently running processes to the test log.

JavaScript, JScript

function Test()
{
  var Enum = Utils.Enumerator(Sys);
  while (!Enum.AtEnd)
  {
    Log.Message(Enum.Item.Name)
    Enum.MoveNext()
  }
}

Python

def Test():
  Enum = Utils.Enumerator(Sys)
  while not Enum.AtEnd:
    Log.Message(Enum.Item.Name)
    Enum.MoveNext()

VBScript

Sub Test

  Set enumproc = Utils.Enumerator(Sys)
  Do While not enumproc.AtEnd
    Log.Message(enumproc.Item.Name)
    enumproc.MoveNext
  Loop
End Sub

DelphiScript

procedure Test();
var
  Enum;
begin
  Enum := Utils.Enumerator(Sys);
  while not Enum.AtEnd do
  begin
    Log.Message(Enum.Item.Name);
    Enum.MoveNext();
  end;
end;

C++Script, C#Script

function Test()
{
  var Enum = Utils["Enumerator"](Sys);
  while (!Enum["AtEnd"])
  {
    Log["Message"](Enum["Item"].Name)
    Enum["MoveNext"]()
  }
}

Enumerator Methods

See Also

Utils Object

Highlight search results