Parameterizing Script Routines

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

In TestComplete, you can create a script routine with any number of parameters. You define script routine parameters in the script code.

For example, suppose you want to create a routine called MyRoutine, that has three parameters: Param1 (string), Param2 (integer), and Param3 (boolean). To do this, use the following script code:

JavaScript, JScript

function MyRoutine(Param1, Param2, Param3)
{
  // ...
}

Python

def MyRoutine(Param1, Param2, Param3):
  # ...

VBScript

Sub MyRoutine(Param1, Param2, Param3)
  ' ...
End Sub

DelphiScript

procedure MyRoutine(Param1, Param2, Param3);
begin
  // ...
end;

C++Script, C#Script

function MyRoutine(Param1, Param2, Param3)
{
  // ...
}

Note: You do not need to specify the parameter types in the script. By default, the type of parameters is Variant. It means that the parameters can hold any Variant-compatible value: string, integer, date, object, and so on.

In the routine code, you can work with routine parameters as you work with local script variables. For instance:

JavaScript, JScript

function MyRoutine(Param1, Param2, Param3)
{
  // ...
  // Post the Param1 parameter value to the test log
  Log.Message("Message: " + aqConvert.VarToString(Param1));
  // ...
}

Python

def MyRoutine(Param1, Param2, Param3):
  # ...
  # Post the Param1 parameter value to the test log
  Log.Message("Message: " + aqConvert.VarToString(Param1))
  # ...

VBScript

Sub MyRoutine(Param1, Param2, Param3)
  ' ...
  ' Post the Param1 parameter value to the test log
  Call Log.Message("Message: " + aqConvert.VarToString(Param1))
  ' ...
End Sub

DelphiScript

procedure MyRoutine(Param1, Param2, Param3);
begin
  // ...
  // Post the Param1 parameter value to the test log
  Log.Message('Message: ' + aqConvert.VarToString(Param1));
  // ...
end;

C++Script, C#Script

function MyRoutine(Param1, Param2, Param3)
{
  // ...
  // Post the Param1 parameter value to the test log
   Log["Message"]("Message: " + aqConvert["VarToString"](Param1));
  // ...
}

To run a parameterized script routine

You cannot run parameterized script routines directly from the Code Editor. To run a parameterized routine, you must pass parameters to it. Thus, you can run parameterized routines in the following ways:

As a test item
  1. Open the Test Items page of your project. To do this, select View > Organize Tests from the TestComplete main menu.

  2. On the Test Items page, add a test item that will run your parameterized routine.

  3. In the Parameters column of the created test item, click the ellipsis button and specify the desired parameter values in the resulting Test Parameters dialog:

    Specify routine parameter values in the Test Parameters dialog

    Click the image to enlarge it.

  4. Run the test item.

    You can do this manually from the TestComplete UI by clicking Run Focused Item on the toolbar of the Test Items page:

    Run the parameterized test item

    Click the image to enlarge it.

    You can also run the entire project (manually, from the TestComplete UI, or automatically, from the TestComplete command line). The project will execute all its test items, including your parameterized routine. See Running Test Items.

From another routine
  1. Create a routine that will call your parameterized routine.

  2. Specify the desired parameter values to pass to your routine within parentheses.

  3. Run the routine that calls your parameterized routine.

For example, in the code below, the Main routine calls the parameterized MyRoutine routine with the specified parameter values:

JavaScript, JScript

function Main()
{
  // ...
  MyRoutine("My String Parameter", 10, true);
  // ...
}

function MyRoutine(Param1, Param2, Param3)
{
  // ...
}

Python

def Main():
  # ...
  MyRoutine("My String Parameter", 10, True)
  # ...

def MyRoutine(Param1, Param2, Param3):
  # ...

VBScript

Sub Main
  ' ...
  Call MyRoutine("My String Parameter", 10, True)
  ' ...
End Sub

Sub MyRoutine(Param1, Param2, Param3)
  ' ...
End Sub

DelphiScript

procedure MyRoutine(Param1, Param2, Param3);
begin
  // ...
end;

procedure Main();
begin
  // ...
  MyRoutine('My String Parameter', 10, true);
  // ...
end;

C++Script, C#Script

function Main()
{
  // ...
  MyRoutine("My String Parameter", 10, true);
  // ...
}

function MyRoutine(Param1, Param2, Param3)
{
  // ...
}

From a keyword test
  1. In your project, create a keyword test that will call your parameterized routine.

  2. Add a Run Script Routine or Run Test operation to the test.

  3. Select your parameterized routine in the operation’s Select Test dialog.

  4. In the resulting Operation Parameters dialog, specify the desired parameter values the operation will pass to your routine when running it:

    Specify routine parameter values in the Operation Parameters dialog

    Click the image to enlarge it.

  5. Run the keyword test:

    Run the parameterized test

    Click the image to enlarge it.

When calling a parameterized routine, instead of specifying hard-coded parameter values, you can specify variables that hold the needed values. See Parameterizing Tests Using Variables.

See Also

Parameterizing Tests
Parameterizing Keyword Tests

Highlight search results