Creating code templates

When coding tests, we are using different programming constructs (for example, if…else, try…catch, and so on). Some of them are used rarely, while others, conversely, are used very often.

TestComplete allows us to accelerate input of some of the programming constructs with the help of so called code templates.

Several of the templates are already predefined in TestComplete; however, they do not always suffice for the job. That is why we will learn how to apply the preset templates and to create some of our own.

How to do it...

Suppose we would like to create a template of our own for the if…else block of JScript code:

  1. Select the Tools | Options menu item, and in the opened dialog window, select the Panels | Code Editor | Code Templates option.
  2. In the opened right-hand window panel select the target language that we want to add a new template for.
  3. Click on the Add button.
  4. Enter the name and description of the new template in the list of templates (to change the inputted values into the table, it is enough to make a single mouse-click on the cell).
  5. In the field under the list, enter the code of the script that should correspond with the template in view.

    Note

    Please take note of the symbol |(the pipeline): it defines the spot for the cursor to appear after the template has been inserted into the editor.

  6. Click on the OK button.
  7. Now, proceed to open any unit and press the following keys combination: Ctrl + J. You will see a listing of available templates on the screen.
  8. Select the newly created template ifelse to witness exactly the same code appear in the editor as previously inputted in the listing of the templates.

How it works...

We have added a new code template for the if...else construction for JScript language, and it can now be used in any TestComplete project.

Please note that code templates are different for all languages, so if you want to add the same construction for C++Script language, you need to repeat the preceding steps again.

In the following screenshot you can see how our template will look after performing all these steps:

How it works...

There's more...

Just as TestComplete settings, code templates are possible to save onto the file on a HDD to be used afterwards on another computer. It can be done using the Save to File and Load From File buttons in the templates' settings panel.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.104.183