Organizing Loops

Applies to TestComplete 14.71, last modified on April 22, 2021

Loops are intended for executing one or more statements repetitively. TestComplete provides two specific operations that allow you to organize loops from your keyword tests: For Loop and While Loop. The following topic describes loop structures that you can organize in your keyword tests using these operations.

Organizing Loops Using the For Loop Operation

Using the For Loop operation, you can repeat statements for a specified number of times. For detailed information, see the operation's description.

The following sample instruction explains how to configure the For Loop operation that will post a number of messages to the test log.

  • Add the integer i variable to your keyword test. This variable will be the loop's counter.
  • Add the For Loop operation. This will invoke the Operation Parameters dialog.
  • Specify i as Loop Variable.
  • Specify 0 as Start Value.
  • Specify 10 as End Value.
  • Specify 2 as Step.
  • Add the Log Message operation as a child operation of For Loop. This operation will be the loop's body. In the ensuing dialog, press Next.
  • Specify i as the Str parameter.

While playing back, the configured test will post "0 2 4 6 8 10" to the test log.

Organizing Loops Using the While Loop Operation

Using the While Loop operation, you can repeat statements until the specified condition is broken. For detailed information, see the operation's description.

The following step-by-step instruction explains how to create a keyword test that will post messages to the test log until the specified condition is false.

  • Add the integer i variable to your keyword test and set its default value to 5.
  • Add the While Loop operation to your keyword test. This will invoke the dialog where you specify the condition to be checked on each iteration.
  • Specify the following conditional statement: i not equal to 0. For detailed information on specifying conditions, see the operation's description.
  • Specify the loop body by adding operations as child operations of While Loop. Add the Log Message operation. Press Next.
  • Specify i as the Str parameter.
  • Add the Set Variable Value operation. In the ensuing dialog, select i and press Next.
  • In the Mode box, select Code Expression and specify the following expression in the Value box: (KeywordTests.MyTest.Variables - 1)

While playing back, the configured test will post "5 4 3 2 1" to the test log.

Highlight search results