Iterating Through Items Collection

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

Application objects and controls can contain properties that return an item collection. These collections can specify, for instance, the list of items of a combo box control, provide scripting access to child objects of the given tree view control, etc. You may need to iterate through item collections in your keyword tests to process all of the items in a certain way.

In order to iterate through a collection of items from your keyword tests, you should do the following:

  • Organize a loop to iterate through a collection.
  • Obtain a certain item of a collection or a certain item's information. To do this, pass the loop counter value to the enumerated property retrieving the item or the item's information.
  • Process the obtained item or item information in a special way.

Here are step-by-step instructions that explains how to configure a keyword test that will obtain the total number of combo box items, iterate through their collections, obtain items' text and post it to the test log:

  • Add integer variables, ItemCount and i, to your keyword test. The ItemCount variable will specify the total number of combo box items. The i variable will be the loop counter.
  • Add the Set Variable Value operation. In the ensuing dialog, select the ItemCount variable from the list of available variables. Press Next.
  • Select Code Expression Mode in the Mode box and enter the script statement that will specify the index of the last combo box item in the Value box:
    (ComboBoxObj.wItemCount-1)
  • Add the For Loop operation to your keyword test.
  • In the ensuing dialog, specify i as Loop Variable, 0 as the Start value parameter (since item indexes are zero-based) and ItemCount as the End Value parameter.

Now, add the operations that will make up the loop's body. They must be added as child operations of the For Loop operation. These operations will be executed repetitively for the number of times specified by the ItemCount counter.

  • Add the Call Object Method operation as a child operation of the For Loop operation. On the first page of the wizard, specify the desired combo box object. Press Next.
  • Select the wItem[Get] property from the list of available properties and methods. Press Next.
  • Specify the i variable as the Item parameter of the property. Press Finish.
  • Add the Log Message operation as a child operation of For Loop. In the ensuing dialog, press Next.
  • Specify Last Operation Result as Str and press Finish.

The following image shows a sample keyword test that iterates through a collection of combo box items and posts their text to the test log:

Highlight search results