TestComplete Script Extensions: StringUtils Object

One of the questions that TestComplete users ask the most is how they can organize libraries of common routines. Until recently, the only option was to share scripts containing common routines with each TestComplete project that needed them. However, the new script extensions technique makes using common script libraries a lot simpler — you can create an extension out of your common scripts and make the routines available to TestComplete projects via a custom object name.

In this article, I’ll show you how to add custom objects to TestComplete with script extensions. Even if you don’t plan to create script extensions yourself, you can just download, install and use the extension - it’s free.

Planning the StringUtils Object

I’m going to create a very simple extension that provides a scripting object. This object, StringUtils, will contain three methods (you can think of them as common routines):

  • CreateGUID will generate a GUID string.

  • Join will concatenate an array of strings into a delimited string.

  • Split will split a string by a delimiter and return an array of substrings.

The Join and Split methods, in fact, will be of interest to those of you who write tests in DelphiScript, because other languages have the built-in functions with the same functionality.

Let’s now see how we can actually implement this object.

Writing the Object Code

To create an object, we need to write a script that implement the object’s methods. The methods are implemented as ordinary script routines, which get executed whenever a method is called from a TestComplete script. A routine’s interface (i.e., declaration) must be exactly the same as the corresponding method, except for the name. That is, if a method is supposed to have parameters, the routine must have these parameters as well; if a method is supposed to return a value, the routine must return a value.

Here’s the full source code of the StringUtils methods (in VBScript):

' StringUtils.vbs

Function StringUtils_CreateGUID

Dim TypeLib

Set TypeLib = CreateObject("Scriptlet.TypeLib")

StringUtils_CreateGUID = Mid(TypeLib.Guid, 2, 36)

End Function

Function StringUtils_Join(StringArray, Delimiter)

StringUtils_Join = Join(StringArray, Delimiter)

End Function

Function StringUtils_Split(String, Delimiter)

StringUtils_Split = Split(String, Delimiter)

End Function

The StringUtils_CreateGUID routine (which will be exposed as the StringUtils.CreateGUID method) uses a common scripting technique to generate a GUID. Namely, it uses the Scriptlet.TypeLib ActiveX object. The Guid property of this object holds a unique GUID for each object instance. However, this property returns a GUID in the registry format (enclosed in braces), so I additionally call VBScript’s Mid function to extract a plain, undecorated GUID.

The StringUtils_Join and StringUtils_Split functions (the future Join and Split methods of the StringUtils object) are wrappers over VBScript’s Join and Split functions. The StringUtils_Join concatenates array elements into a single string, where elements are delimited by a specific character; the StringUtils_Split splits a string by a delimiter and returns substrings in an array. You can find more info on these functions in the VBScript Reference at MSDN.

Creating the Description File

The object code is ready, but how does TestComplete know that this code represents an object? Each script extension must include a special XML file, description.xml, which exposes information about the extension’s functionality to TestComplete. Here’s the listing of the description.xml file that defines the StringUtils object and its methods:

<?xml version="1.0" encoding="UTF-8"?>

<ScriptExtensionGroup Name="StringUtils Object">

<ScriptExtension Name="StringUtils Object" Version="1.0"

Author="Helen Kosova" HomePage="www.automatedqa.com">

<Script Name="StringUtils.vbs">

<RuntimeObject Name="StringUtils">

<Method Name="CreateGUID" Routine="StringUtils_CreateGUID">

Generates and returns a new GUID string.

</Method>

<Method Name="Join" Routine="StringUtils_Join">

Concatenates array elements into a delimited string.

</Method>

<Method Name="Split" Routine="StringUtils_Split">

Splits a delimited string into an array.

</Method>

<Description>

The StringUtils object provides helper string routines.

</Description>

</RuntimeObject>

</Script>

<Description>

Provides the StringUtils object.

</Description>

</ScriptExtension>

</ScriptExtensionGroup>

This file is more interesting than the object’s source code, so let’s take a closer look at its contents:

<ScriptExtensionGroup  Name="StringUtils Object">

<ScriptExtension Name="StringUtils Object" Version="1.0"

Author="Helen Kosova" HomePage="www.automatedqa.com">

...

<Description>

Provides the StringUtils object.

</Description>

</ScriptExtension>

</ScriptExtensionGroup>

The first lines define the name of the extension package and the extension itself. In general, a package can contain more than one extension, but I only included one extension, so I named the extension and the package the same. Also, here you can see some other information such as the extension version and the author’s name. The Description tag specifies comments about the extension. All of this information will be displayed in TestComplete’s script extensions list.

<Script Name="StringUtils.vbs">

...

</Script>

This tag specifies the name of the script file that contains the StringUtils object implementation.

<RuntimeObject Name="StringUtils">

<Method Name="CreateGUID" Routine="StringUtils_CreateGUID">

Generates and returns a new GUID string.

</Method>

<Method Name="Join" Routine="StringUtils_Join">

Concatenates array elements into a delimited string.

</Method>

<Method Name="Split" Routine="StringUtils_Split">

Splits a delimited string into an array.

</Method>

<Description>

The StringUtils object provides helper string routines.

</Description>

</RuntimeObject>

The RuntimeObject tag is used to define an object that is made available by the extension. Its child Description tag specifies the object description, which will be displayed in TestComplete’s Code Completion window.

The Method tags define the object methods and specify the underlying script routines. There’s no need to define the method parameters -- TestComplete will retrieve information on them automatically. The contents of the Method tags will be used as the methods’ descriptions in the Code Completion.

Packaging the Extension

Unlike “traditional” plug-ins that need to be compiled, the extension’s source files just need to be packed into a ZIP archive. The archive extension should then be changed to tcx (TestComplete extension). That’s all, no other installation preparations are needed.

You can download the package from this link:

» Download the StringUtils extension

Installing the Extension

Installing the extension could not be simpler. Just double-click the .tcx package file and choose the folder where you want to install it. You can customize the list of folders where extensions can be stored in TestComplete’s Engines | Script Extensions settings, or add new paths right in the installer. The default path is <TestComplete>\Bin\Extensions\ScriptExtensions.

Using the StringUtils Object

Once the extension is installed in TestComplete, the StringUtils object is available for use. It is now displayed in the Code Completion window with the other objects (note comments about the object and its methods, retrieved from the description file):

The StringUtils object in Code Completion

In the script code, StringUtils can be used like any other TestComplete object. Here’s a DelphiScript snippet that demonstrates the usage of the StringUtils methods:

// [TestComplete DelphiScript]

procedure TestStringUtils;

var arr, arr2, str, i;

begin

// Generate a GUID

Log.Message(StringUtils.CreateGUID);

// Convert an array into a string

arr := ['a', 'b', 'c'];

str := StringUtils.Join(arr, ', ');

Log.Message(str);

// Split a string into an array of strings

arr2 := StringUtils.Split(str, ', ');

for i := 0 to VarArrayHighBound(arr2, 1) do

Log.Message(arr2[i]);

end;

Just copy and paste this code into a DelphiScript project (provided that you have the StringUtils extension installed), and try it out! For those of you who can’t do this right now, the image below shows a sample output:

The TestStringUtils routine's log

Conclusion

As you can see, creating custom objects and common routine libraries with the script extension technique is a lot easier than creating traditional plug-ins from a SDK. Since these extensions are created using scripting languages (VBScript or JScript), they do not require programming skills beyond script writing to implement custom objects. With minimum efforts you can define the custom object’s interface, create the extension package and install the extension into TestComplete.

The StringUtils extension described in this article can be downloaded from here. I hope you enjoy using it as much as I enjoyed creating it!

Next time, I’ll demonstrate how to add a new toolbar command to TestComplete that will generate the “wait for object” code with a single click. Stay tuned!


Close

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

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