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
-
From the box on the top of the dialog select the scripting language for the template.
-
Click Add. TestComplete will add a new template to the template list at the top of the dialog.
-
Specify the template name and description in the list column.
-
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
-
Select Tools | Options from the TestComplete main menu to display the Options dialog.
-
Choose Panels | Code Editor | Code Templates from the tree in the dialog to display the Code Template’s options page.
-
In the Language box, select the scripting language for the desired template.
-
Choose the desired code template in the template list and click Delete. To remove all templates, click Clear.
-
Press OK to save your changes and close the dialog.