Code Templates

Applies to TestComplete 14.30, last modified on November 21, 2019

Code Templates (also known as code snippets) allow you to quickly insert ready-made statements into your script code. This topic provides complete information on working with code templates. The following items are located throughout this topic:

Insert Code Templates

To insert a code template into your script code, do any of the following:

  • Type the name of the desired template in the Code Editor and press Tab.

– or –

  • Press Ctrl+J within the Code Editor. TestComplete will open a list box with the available templates:

  • Select the desired template in the list and press Enter.

  • If the selected code template has one or several parameters, TestComplete will ask for their values:

TestComplete will insert the selected template into the cursor’s current position.

Note: If the Code Templates list is empty, the templates list box will not appear. Instead, TestComplete will show a message asking whether you want to define the code templates.

After a code template has been inserted, you can edit and delete its code as you would with any other code. That is, code templates do not insert special objects, they just insert ordinary text which then can be edited. For instance, to undo the code template insertion you can press the Undo shortcut.

Create or Modify Code Template

To create a new or modify an existing code template, use the Code Template’s options. To view or modify them, select Tools | Options from the TestComplete main menu and then choose Panels | Code Editor | Code Templates from the tree in the ensuing Options dialog.

To create a new template
  1. From the box on the top of the dialog select the scripting language for the template.

  2. Click Add. TestComplete will add a new template to the template list at the top of the dialog.

  3. Specify the template name and description in the list column.

  4. Modify the template code in the edit box below the template list.

The code of a template can include any statements, comments, identifiers, etc. – anything you would like to quickly and easily insert into your script code.

To modify a template

To modify a template, simply select it in the templates list of the dialog and then change the template code in the edit box.

To specify the cursor position

To specify the cursor position after the template is inserted, use the vertical line (“|”). For instance, if you insert a template having the following code –

function Test(){
return | ;
}

– TestComplete will position the cursor after the return word.

To specify parameters

To specify parameters, enclose their names in % characters, for instance:

for (%Variable% = %StartValue%; %Variable% < %EndValue%; %Variable%++){
|
}

This example template has three parameters: Variable, StartValue and EndValue. TestComplete will ask for them when you are inserting the template.

Delete Templates

  1. Select Tools | Options from the TestComplete main menu to display the Options dialog.

  2. Choose Panels | Code Editor | Code Templates from the tree in the dialog to display the Code Template’s options page.

  3. In the Language box, select the scripting language for the desired template.

  4. Choose the desired code template in the template list and click Delete. To remove all templates, click Clear.

  5. Press OK to save your changes and close the dialog.

Share Code Templates

You can easily share the templates you created or modified with other users. All templates are stored in the Windows Registry on the computer where they have been created. To share the code templates, save them to a file and then send these files to the desired users.

Sharing Templates for a Specific Language

To export code templates for a specific language:
  1. Select Tools | Options from the TestComplete main menu to display the Options dialog.

  2. Choose Panels | Code Editor | Code Templates from the tree in the dialog to display the Code Template’s options page.

  3. In the Language box, select the scripting language whose templates you want to export.

  4. Click Save to File and save your code templates to a file (the file will hold code templates defined for the selected scripting language).

  5. Send the file to the desired users or share the access to the file.

To use the exported code templates file on another computer:
  1. Launch TestComplete.

  2. Call the Options dialog and open the Code Templates Options page.

  3. In the Language box, select the language whose templates you want to load.

  4. Press Load From File and in the Open File dialog choose the file that you stored code template settings. TestComplete will load these settings from the file and write them to the Registry.

  5. Press OK to save your changes and close the Options dialog.

Sharing Templates for All Languages

To export code templates for all scripting languages:
  • Select Tools | Settings | Export Settings from the TestComplete main menu. This will call the Export Settings dialog.

  • In the dialog, check Code Templates and uncheck other items.

  • In the Settings file name box, specify the name of the file to which the templates will be saved.

  • Press Export.

  • Send the resulting file to your colleagues or share the access to the file.

To import the code templates on another computer:
  • Launch TestComplete.

  • Select Tools | Settings | Import Settings from the TestComplete main menu.

  • In the ensuing Export Settings dialog, click Browse and choose the file that holds the exported templates.

  • Make sure that the Code Templates item is checked in the list of settings to import, and click Import.

See Also

Code Editor
Code Completion

Highlight search results