New TestComplete Script Extension - Expand Expression

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:

// [JScript]


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:

// [JScript]


s = ExpandExp.Exec("Computer name: %Sys.HostName%, Memory usage: %Sys.MemUsage%%");

Log.Message(s);

As you can see, the code that constructs the desired string looks a great deal simpler.

The 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 Exec method:

// [JScript]

// Inserts the value of the Sys.OSInfo.FullName property

s1 = ExpandExp.Exec("The operating system is %Sys.OSInfo.FullName%.");

// Inserts the value of the MyVar project variable
// Note that we don't specify the variable type.
// The method finds the variable automatically.

s2 = ExpandExp.Exec("The MyVar variable contains the %MyVar% value");

Download and Installation


  1. You can download the Expand Expression package from the AutomatedQA web site:

    » http://www.automatedqa.com/images/blog/ExpandExpression/ExpandExpression.tcx

  2. After you have downloaded the file to your computer, double-click it in Windows Explorer. This will invoke the Installing Extension dialog.

     
  3. In the dialog, specify the folder where the extension will be installed and click OK.

     

  4. 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

The ExpandExp.Exec method has the following parameters:

ExpandExp.Exec(inputString, [variablesPriority])

  • 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.

The 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:

// [JScript]


Project.Variables.FileDirectory = "C:\Tests";

Project.Variables.FileName = "%FileDirectory%\sample.txt";

s = ExpandExp.Exec("The full path to the file is %FileName%");

Log.Message(s);

Restrictions

  • Nested expressions in one string expression are not supported (for example, %Var1%Var2%%).

  • Script variables (both local and global) cannot be used in the input string.

  • The 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:

    // [JScript]


    s = ExpandExp.Exec("tblVar[0, 1] = %Project.Variables.tblVar(0, 1)%.");

Conclusion

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.


Close

Add a little SmartBear to your life

Stay on top of your Software game with the latest developer tips, best practices and news, delivered straight to your inbox

By submitting this form, you agree to our
Terms of Use and Privacy Policy

Thanks for Subscribing

Keep an eye on your inbox for more great content.

Continue Reading