In a previous article, I’ve discussed (among other things) the possibility of adding COM objects to the QTP reserved objects pool, thereby enabling some limited sort of intellisense for them (i.e. Syntax auto-complete).
This is all very nice, but what happens when we want to do something similar with our custom classes and objects? It seems that since the process involves specifying the object’s COM ProgID, we can only add "proper" objects to the QTP reserved objects pool (e.g. Excel.Application or Scripting.Dictionary objects). For those of us who use custom classes and object on an everyday basis, this becomes a huge limitation on the method.
Well, have no fear, a solution is here. MilindCD from SQAForums has published a solution which allows us to write our own classes, create a COM wrapper around them, and then add them to the QTP reserved objects pool. It can be quite cool as it allows us to create custom extensions of VBScript’s "regular" objects, and use them with a kind of limited intellisense.
Let’s go through the solution and see how it’s done.
The Specifics
First of all, a little disclaimer, this will only give you VERY limited intellisense. If you want true intellisense in QTP you’ll either have to either bang your had against the wall in frustration, or install Test Design Studio, which extends QTP, and enables intellisense similar to that in Visual Studio.
Now we can move on to the specifics. First of all we need to introduce a new type of file: Windows Script Component. For our purpose, a windows script component (WSC) is just an XML file with a VBScript class contents and some Meta-data which describes the code. So it’s the plain old VBScript code you’ve known and loved, with some lines that talk about the code and allows windows to better understand it.
What we’re going to do is to use the Meta-data to register our custom class to COM. Once we’ve done that, we can easily add the new COM object to QTP’s reserved objects pool (in this point, our custom COM object has the same status as a Scripting.Dictionary or Excel.Application "Proper" object), and get instant intellisense for it.
The structure of WSC
We won’t get into ALL the possibilities available in writing WSC files, just the basics needed to register our VBScript class to COM. You can explore the rest on your own in either the MSDN documentation on the matter, or just Google it.
First, it’s an XML file, so the basic skeleton is your run of the mill open-close tag wrapper, with "component" as the central tag (there is a possibility to wrap several components in a single package tag, but we won’t get into that):
<?xml version="1.0″?>
<component id="MyQTPClass">
</component>
The id part is optional (if you only have one component in the file), so we could have just written <component>. I think it’s better to organize our code with identifiers even when it’s not necessary.
Within the component we can add a special instruction which will validate our XML structure before registering the component to COM. This is a very important option to include:
<?component error="true" debug="true"?>
Next comes the part which specifies the COM registration data:
<registration
description="AdvancedQTP Custom Class Example"
progid="AdvancedQTP.CustomClass"
version="1″
classid="{7cf11854-dcf4-40cc-9a11-b365f0a734ef}"
/>
Well, let’s see what we got here. The registration tag is composed of several attributes. Description and Version are optional attributes meant for documentation purposes only. ProgID will be the unique COM name by which our custom control will be identified (i.e., it’s the string you send to the CreateObject method to create your class instance). Classid is a GUID unique identifier which identifies our class in the registry (you can find random GUID generators that will give you a GUID to put in the part – just search GUID generator in Google).
This pretty much gives windows everything it needs in order to register our class to COM. Well, except the actual class code, and how it can be used. Let’s begin with how it can be used. We need to tell COM what are the public properties and methods available in our class:
<public>
<property name="ID" get="ReturnID" put="SetID"/>
<method name="PrintMessage">
<parameter name="Message"/>
</method>
</public>
As you can see, we can describe both the name of the functions and properties, and the parameters they take. By defining both Get and Put elements for the ID property we set it as read/write, but we could’ve only used the read part, or map the property directly to an inner variable. There are many other options available to you through the MSDN documentation.
And now, last but not least, the actual code of our class (ignore the inner line numbers, there’re not part of the code):
<script language="VBScript">1:2: <![CDATA[3: Dim pID4: Function ReturnID()5: ReturnID = pID6: End Function7: Function SetID (sValue)8: pID = sValue9: End Function10: Function PrintMessage(Message)11: Msgbox Message12: End Function13: ]]></script>
Well, it’s pretty much a cut-and-paste version of the script, wrapped in an XML <script> tag and a CDATA section. The only noticeable change is that our property is implemented through functions.
Wrapping it all together
So, to summarize, our complete WSC file should look something like this (ignore the inner line numbers, there’re not part of the code):
<?xml version="1.0″?>
<component id="MyQTPClass">
<?component error="true" debug="true"?>
<registration description="AdvancedQTP Custom Class Example" progid="AdvancedQTP.CustomClass" version="1″ classid="{7cf11854-dcf4-40cc-9a11-b365f0a734ef}"/>
<public>
<property name="ID" get="ReturnID" put="SetID"/>
<method name="PrintMessage">
<parameter name="Message"/>
</method>
</public>
<script language="VBScript">1:2: <![CDATA[3: Dim pID4: Function ReturnID()5: ReturnID = pID6: End Function7: Function SetID (sValue)8: pID = sValue9: End Function10: Function PrintMessage(Message)11: Msgbox Message12: End Function13: ]]>14:</script>
</component>
Now we can save the file, right-click it from windows, and we’ll have the option to register it to COM. Once our class is indeed registered to COM, we can create it in our script via the CreateObject("Advancedqtp.CustomClass") command, or add it to the reserved objects pool as previously described.
WSC has many more tricks and hacks in it, and you can find them by sieving through the extensive Microsoft documentation and general articles available on the net. I hope that this article will provide you with the starting point you need in order to do so.
Posted in QTP Hacks

Yaron Assa




May 6th, 2008 at 1:22 pm
This was really helpful :)
May 9th, 2008 at 11:47 am
The article very good. But i can use it in QTP.Can you show your detail script. thanks!
May 21st, 2008 at 11:55 am
i got it. thanks
August 16th, 2008 at 3:31 pm
[…] there are workarounds for both these issues through the PDM hack (for debug intellisense), and WSC registration (for VBScript Class intellisense through […]
October 22nd, 2008 at 10:41 am
how extraordinary it is!
December 22nd, 2008 at 4:16 pm
This is great.
I’m only struggling with 2 things:
1. I want to show an additional line in the intellisense tooltip for custom function.
In this additional line I want to give short explanation about function.
Is this possible?
2. When the intellisense tool tip for a custom function pops up in QTP then the last parameter is OptionalArgs. But I don’t want that this last parameter is shown. How can I avoid this?
January 30th, 2009 at 7:35 am
Hi,
Thanks for posing the article. This was really helpful. I have some doubts please clarify it. I have created class which has different properties for stroing the UI object details in DP. My class looks like this
I have registered the class and added to regedit for getting intellisense and I am successful in getting intellisense too but when i run the script i get a error message indicating that
“Type mismatch: ‘dialog’”
I am not able to proceed further. Please help me out on this.