Reserved Objects as an Env. Object Replacement
Posted by admin - Mar 30, 2008 Articles, Yaron Assa 0 0 Views : 928 Receive Updates For This Category
Article Tools
- Print this page
- Add Comment
- Send to Friend
- Last Updated on :
Jul 16, 2011
Today we’re gonna look at QTP’s internal mechanism for creating singleton COM objects, called Reserved Objects; And we’ll see how Reserved Objects can be used to improved and simplify our scripts.
Well, that was a heavy title… let’s see if we can break it down:
1. A COM object is any object you can create via CreateObject command (Scripting.Dictionary, Word.Application, etc.)
2. Singleton means that only a single instance of the reserved object can be created.
3. Internal mechanism means that if you’re gonna use it, it will include some registry tweaking.
Now that we’ve simplify things a bit, let’s move on.
QTP has a neat mechanism called Reserved Objects. Any COM object can be made into a Reserved Object, and will be accessible via QTP from there on.
So, why not create that object via CreateObject method, assign it to a variable, and just use it regularly? Reserved Objects have two major benefits:
1. They are created ONCE when a QTP script starts running, and are globally accessible. So they’re kinda like declaring a global variable, but in a much stronger, deeper sense. It can solve may of the pesky problems of unclosed system processes due to unclosed CreateObjects objects
2. Once an object is added to the reserved objects pool, it’s accessible via QTP’s auto-complete. This means that both the object’s name, and internal methods & properties will be auto-completed. Which is kinda cool.
Reserved Object can be used to simplify and organize many tasks: file IO, log writing, using excel / word / outlook etc. I specifically love using Reserved Objects to replace the Environment object.
The environment object is supposed to give you a global data storage, but I just find it too restrictive. It can only house simple variable types, which means you can forget about passing objects globally; There is no simple way for looping through all the values; And many more annoying “features”.
So, instead of using the Environment object, I create my own Scripting.Dictionary reserved object (as seen in the screen captures), which allows me to host any number of sub-values, ranging from a string, a QTP object, a nested dictionary, to just about anything. I can then loop through the values by numbers, keys, or items, and have complete freedom in my code. I know it’s kinda hard to notice, but I REALLY love the dictionary object…
So, how do we actually create reserved object?
This part is all about editing the registry. AdvancedQTP takes no direct or indirect responsibility registry tweaking. Do this at your own risk, and you better know what you’re doing…
But ’nuff chit-chat.
Here’s the thing: Open your registry and go to :
HKEY_CURRENT_USER\Software\Mercury Interactive\QuickTest Professional\MicTest\ReservedObjects\
There you’ll see many of QTP’s internal object. DO NOT MESS WITH THEM.
Next, create a new key, with the name of the reserved object (e.g. Advanced QTP)
Enter the key “folder”, and create these values :
ProgID (string) – Holds the COM program ID we want to create (e.g. Scripting.Dictionary)
VisibleMode (dWord) – Holds the value 2. This controls the auto-complete and intellisense appearance.
UIName (string) – The name that will refer to the reserved object. Make sure this is the same as the key name.
There are more properties, which control the icon of the object, and other options. Sadly, I while trying to figure them out, I’ve messed my QTP a bit, so I’ve stopped experimenting. If you know of more properties, please let me know.
And that’s it. The next time you’ll open QTP, the reserved object will be accessible through the auto-complete from all the scripts and functions.
Have fun


