Test Design StudioGo to Previous Topic: UFT Object Repository IntelliSenseGo to Next Topic: Outlining (Code Folding)

Code Snippets

Code Snippets are used to store pieces of code that are used often. You activate a Code Snippet in three ways:

When a snippet is activated, the text for the snippet is automatically placed within the editor and your selection moves to the first field in the snippet. Fields are used to mark portions of the snippet that should be easily modified by the user. For example, the following code snippet shows a VBScript property declaration for a class complete with a backing field:

Tip:   If text is selected in the editor when a snippet is activated and that snippet supports surrounding text, the selected text will be embedded in the snippet. Activating a for snippet, for instance, will take the selected text and move it to the body of the For loop.

Fields

In the sample above, you have four fields; VariableName, PropertyName, ByVal, and vData. The VariableName field occurs three times, PropertyName occurs three times, ByVal occurs only once, and vData occurs twice. You will notice The VariableName field is currently active, the two additional dependent fields are denoted with a dashed border. If the value of a field is modified, all dependent fields are updated to refelect the same value. The ability to modify one field and have all dependent fields instantly updated to match is what gives code snippets their power and elevates them above basic template text that is inserted by editors such as Unified Functional Testing.

Navigation

All standard text editing capabilities are available while a code snippet is active. You can freely modify any of the text that is automatically inserted by the snippet. The following keyboard keys can be used to interact with an active code snippet:
KeyFunctionality
TabWhen the caret is positioned inside an active field, the Tab key will move you to the next field. The text of that field will be automatically selected so that it can be easily replaced.
EnterWhen the caret is positioned inside an active field, the Enter key will exit code snippet mode and move the caret to the end position of the snippet (this is defined by the $end$ keyword in the code snippet text described below).
EscExits code snippet mode without modifying the location of the caret.

The typical process for populating a code snippet is the following:

Commands

The following commands are available for working with code snippets. Both are available from the Edit -> IntelliSense menu.
CommandDescription
Insert SnippetDisplays the Insert Snippet popup allowing you to type or select a code snippet to insert.
Surround WithDisplays the Surround With popup allowing you to type or select a code snippet to insert. Only snippets that support surrounding text will be displayed.

Insert Snippet Prompt

The following prompt is displayed when the Insert Snippet command is executed.

A form with equivalent functionality is displayed for the Surround With command except only snippets support surrounding text will be displayed.

A member list will be displayed showing all the available snippets. You may type directly in the prompt to quickly access the snippet, or you may use a combination of the cursor keys and mouse to select a snippet. Code snippets can be grouped by folders for organization, so you may need to first select a folder to find the appropriate code snippet.

Code Snippet Manager

Code Snippets are maintained by the Code Snippet Manager (accessed by selecting Tools -> Code Snippet Manager from main menu).

Code snippets are targeted for a specific language and organized by folders. Select the desired language from the drop-down list of languages and the available code snippets will be displayed. Select any folder or code snippet to see the location of the respective item in the Location textbox. You may also select any code snippet to view details about the item in the right pane.

The structure of folders is maintained in a browsable state when you use the Insert Snippet or Surround With IntelliSense options.

Built-in Code Snippets and Defaults

By default, there will always be a built-in folder for each language, and it cannot be removed. There will also be a My Code Snippets folder that is intended for the storage of personal code snippets (select the My Code Snippets folder and the Location textbox will reveal the path to the folder). This folder is pre-defined for convenience and may be easily removed. If a folder appears with an exclamation mark over the icon, that folder does not exist on the file system and can be removed if desired. You can always add your own folder to the language by clicking the Add button and browsing to the folder you wish to add.

Adding a Folder

Code snippets are special files stored on the file system. You can use the Add button to add any folder to the currently selected language. This will automatically import all code snippet files from that folder as well as recursively add the snippet files from any sub-folders (if present). Since all sub-folders are automatically imported, you will only need to add the top-most folder.

Removing a Folder

To remove a user-defined folder, highlight the top-most folder and click the Remove button. The child folders of a top-most folder may not be individually removed since they are auto-imported with the top-most folder. To remove a child folder, you would need to physically remove it from the file system to prevent it from being automatically imported.

Note:   Built-in folders cannot be removed.
Tip:    The name of a code snippet folder is derived from the name of the folder on the file system. You can override the name of a folder or sub-folder by placing a SnippetDir.xml file within that folder. The contents of that file should read as:
[XML] - Copy Code
<SnippetDir>
  <Name>Specify New Name of Folder</Name>
</SnippetDir>
This will override the name of the folder with the name specified in the file. This is especially useful when multiple folders of the same name are added for a language so that you can differentiate between the folders.

Refreshing a Folder

Code snippet data is cached when the application is launched. If any changes are made to snippet files or the underlying file system, you must select the respective folder and click the Refresh button to updated the cached data.

Editing a Code Snippet

To quickly edit any code snippet, select the code snippet you would like to edit and click the Open Snippet button. The Code Snippet Manager will close, and the selected code snippet will be opened in the Code Snippet Editor.

Creating a New Code Snippet

To create a new code snippet in an existing folder, first select the desired folder or select any code snippet that is already contained within the desired folder. Then click the New Snippet button. You will be prompted to provide a file name for the new snippet, the Code Snippet Manager will close, and the new snippet will be opened in the Code Snippet Editor. Set the necessary information and save your new code snippet. It will become immediately available to Test Design Studio.

Special Language Handling

Tip:    The language Unified Functional Testing is a derivatives of the VBScript language. As a result, all VBScript code snippets will always be available to the Unified Functional TestingPro language. Any code snippets that are generic to VBScript should be placed under the VBScript language. The Unified Functional Testing language should include snippets specifically for use with Unified Functional Testing.

Code Snippet Editor

Even though code snippet files are stored as XML-formatted text, a special Code Snippet Editor has been designed for interacting with code snippet files. Click here for details about the use of that editor.

Tip:    The snippet files used by Test Design Studio are fully compatible with most snippet files used by Microsoft®'s Visual Studio. Therefore, files created for use in Visual Studio may also be used within Test Design Studio. Visual Studio snippet files using function references are not supported by Test Design Studio.