You have the option of creating your own code snippets in existing folders and/or adding additional folder locations where code snippets are stored. Our Walkthrough project contains a folder of code snippets that have been previously created. We want to include this folder in our code snippet collection for the VBScript language.
Note: Some of the screen shots below are from an older version of Test Design Studio that may not visually match the current version. Just follow along with the instructions and do not be alarmed if your screen does not exactly match that of a screen shot.
Open the Code Snippet Manager by selecting Tools -> Code Snippet Manager from the main menu.
Note: You cannot add a specific code snippet file to the Code Snippet Manager. You have to add the folder the code snippet is contained within. Any sub-folders are automatically added as well.
Within the Code Snippet Manager, make sure the VBScript language is selected. Click the Add... button to add a new folder.
Browse to and select the Code Snippets folder at the following location:
C:\Test Design Studio Samples\Walkthrough\Walkthrough\Code Snippets. Once the folder is selected, click the OK button to close the Browse for Folder dialog. The new folder will be added to the list of folders for the VBScript language as Code Snippets from Walkthrough Project.
By default, all code snippets folders are given the name of the folder itself. In this situation, we changed the name by including a special
SnippetDir.xml file under that folder. When present, the content of that file will be used to determine the name of the code snippet folder.
Expand the Code Snippets from Walkthrough Project folder and you will see that the sub-folder Property Declarations was automatically included. Expand this folder and you will see the three property declaration code snippets that have been added for declaring specific types of properties. Click the OK button to close the Code Snippet Manager.
To test the new declarations, open or return to the
CodeSnippets.qfl file created in the earlier example. Place the caret on an appropriate line in the class declaration to insert a new property declaration. Instead of using the shortcuts, right-click on the text editor and select Insert Snippet from the context menu. You will notice a new grouping called Code Snippets from Walkthrough Project. Activate that group and the Property Declarations group is now displayed. Activate the Property Declarations group and the custom property declaration code snippets are displayed. Select and activate the Declare a String Property code snippet.
If you recall, the original "Declare a Property" code snippet used one field to define the name of the property, and another to define the name of the backing variable. This code snippet has been simplified to derive the name of the backing variable from the name of the property. Since this property represents a string value, we will simply prefix our variable name with an
s (which is a common convention for naming a variable that holds string data, others may use
str). We also see the inclusion of some XML comments which will be used by Test Design Studio to describe the property. Provide a name for the property and hit the Enter key. We now have a full declaration for a string-based property which required very little text entry!
We have pre-defined property code snippets for String, Boolean, and Integer data types. Let's create a new Code Snippet for Date types. In Solution Explorer, right-click the Property Declarations folder and select Add -> Add New Item... from the context menu.
Select the Code Snippet template (#1 in the image above). Name the file
propertydate.snippet (#2) and click the Add button (#3).
The Code Snippet editor will now be opened for your new file.
Enter the following values in the Details section:
Tip: We are not using the
$selected$tag in our code snippet, so there is no special functionality for Surround With.
Type or paste the following code into the Code Snippet Text section:
Private d$propertyName$ ''' <value type="Date"/> Public Property Get $propertyName$ $propertyName$ = d$propertyName$ End Property Public Property Let $propertyName$(ByVal $value$) d$propertyName$ = $value$ End Property$end$
If you examine the text, you will see that we have two fields:
$value$. We need to add declarations that will correspond to these fields.
In the Declarations section, click the Add button to add a new declaration. Set the following values for the new declaration:
Note that after setting the ID to
propertyName the text was updated to show the
$propertyName$ field in bold, blue coloring. This indicates that the field in the text is properly declared. By looking at the text, we can see that the
$value$ field still needs to be declared because it is not in bold, blue coloring.
In the Declarations section, click the Add button to add another new declaration. Set the following values:
Save your code snippet and return to the CodeSnippets.qfl document to test your new code snippet. If you try to use the code snippet now, it will not be displayed. That is because the code snippet did not exist in the Code Snippets folder when it was last processed. You need to refresh the folder to pick up any new files. Adding new code snippets is not a common action, so we conserve resources in Test Design Studio by not monitoring the folder for changes. Fortunately, refreshing the folder is easy. Use the Tools menu to open the Code Snippet Manager.
Select the VBScript language, then expand Code Snippets from Walkthrough Project and Property Declarations. Here you can clearly see the new snippet is not displayed. With the Property Declarations folder selected, click the Refresh Folder button.
Your new code snippet will now be displayed. Click OK to close the Code Snippet Manager.
Test out your new code snippet by typing the shortcut
propdate and pressing the Tab key.