When creating test scripts, you often need to replace values of some string variables with those of other ones. A typical example is posting information about a computer name and the memory usage to the test log. To perform this task, you have to write script code that assembles the desired string from a number of smaller strings and property values:
Log.Message("Computer name: " + Sys.HostName + ", Memory usage: " + Sys.MemUsage + "%");
This code may be difficult to understand, especially in case of long messages. An easier approach would be to represent the desired string in the following manner:
Computer name: %Sys.HostName%, Memory usage: %Sys.MemUsage%%
The Expand Expression script extension does this. It provides the
ExpandExp scripting object that allows you to use strings, some parts of which are marked with the % symbols. The object contains only one method –
Exec – that obtains the string for conversion, scans through it, replaces the expressions in the % symbols with their actual value and returns the resultant string.
Using this object and method you can rewrite the abovementioned code in the following manner:
s = ExpandExp.Exec("Computer name: %Sys.HostName%, Memory usage: %Sys.MemUsage%%");
As you can see, the code that constructs the desired string looks a great deal simpler.
Exec method can “expand” property or methods calls as well as references to the project and project suite variables. If an expression in % symbols does not match any property, method or project, or project suite variable, the expression remains unchanged in the resultant string.
Here are some more examples of using the
s1 = ExpandExp.Exec("The operating system is %Sys.OSInfo.FullName%.");
s2 = ExpandExp.Exec("The MyVar variable contains the %MyVar% value");
Download and Installation
You can download the Expand Expression package from the AutomatedQA web site:
- After you have downloaded the file to your computer, double-click it in Windows Explorer. This will invoke the Installing Extension dialog.
- In the dialog, specify the folder where the extension will be installed and click OK.
If you have TestComplete running, then you need to update the list of installed script extensions. To do this:
- Select File | Install Script Extension from TestComplete’s main menu. This will invoke the Script Extensions dialog.
- In the dialog, click Reload. You will see the extension under the Runtime Objects node.
- Close the dialog.
If the extension was installed successfully, you can see the
ExpandExp object in the Code Completion window:
More About the Exec Method
ExpandExp.Exec method has the following parameters:
inputString – The string to be processed. The expressions to be “translated” should be enclosed within the % symbols.
variablesPriority – An optional parameter that specifies the search order for the project and project suite variables. The parameter can be one of the following strings:
- "project|suite" (default) – The method will first search for a variable in a project variable list and then in the project suite variable list.
- "suite|project" – The method will first search in the project suite variable list and then in the project variable list.
Exec method processes the input string iteratively. This means that after all the %% expressions in the input string were replaced with their values on the first iteration, the method processes the resulting string again and again until there is nothing to replace. This feature makes it possible for you to nest one project variable into another. Here is an example:
Project.Variables.FileDirectory = "C:\Tests";
Project.Variables.FileName = "%FileDirectory%\sample.txt";
s = ExpandExp.Exec("The full path to the file is %FileName%");
Nested expressions in one string expression are not supported (for example,
Script variables (both local and global) cannot be used in the input string.
Exec method does not recognize the project and project suite variables of the Table or DBTable types in the same manner as it recognizes variables of the ordinary types. If you want to use these variables in the method call, use the
Project.Variables.variable_name syntax. The following code demonstrates how you can output the item (0, 1) of the project’s
tblVar table variable:
s = ExpandExp.Exec("tblVar[0, 1] = %Project.Variables.tblVar(0, 1)%.");
Try using the Expand Expression script extension in your projects – we hope it will help you create tests easier and faster. The script extension is compatible with TestComplete 7.50 and later versions. If you have not yet seen the latest and greatest release of TestComplete 8, please visit www.automatedqa.com/products/testcomplete/ for product information, upgrades, purchases, or to request a free trial.