Login   /   Register

Reserved Objects as an env. object replacement

Rate this article
     5 votes, average: 5 out of 55 votes, average: 5 out of 55 votes, average: 5 out of 55 votes, average: 5 out of 55 votes, average: 5 out of 5
Loading ... Loading ...
March 30th, 2008 by Yaron Assa

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

Posted in QTP Hacks

18 Responses to “Reserved Objects as an env. object replacement”

  1. Intellisense and COM | AdvancedQTP Says:

    [-]

    […] a previous article, I’ve discussed (among other things) the possibility of adding COM objects to the QTP reserved […]

  2. raman Says:

    [+]

    Its really great and working fine.I just cant stop my self saying thanks.I have been searching for the same kind of thing for many... ...

  3. Yaron Assa Says:

    [-]

    You’re very welcomed

  4. fengyi_3 Says:

    [-]

    very nice!

  5. Manoj Says:

    [-]

    Hats off to you guys….

  6. puneetsh1 Says:

    [-]

    I must say its g8 solution. Thanx a lot..

  7. vishala Says:

    [+]

    I tried creating "Excel.Application" com object with the properties same as you had mentioned ,but the value i had used for ProgID... ...

  8. vishala Says:

    [+]

    Hi...It started working after i re-launched QTP...it is such a wonderful learning for me.Thank you very much for posting such a wo... ...

  9. An improved dictionary object | AdvancedQTP Says:

    [+]

    [...] series of QTips covering it’s basic uses, as well as some advanced articles of using it as a reserved global dictionary, a... ...

  10. sridhardokuparthy Says:

    [-]

    Great work thanx

  11. deepakkhat Says:

    [-]

    Its really Wonderful. Thanks a lot for such useful posts…

  12. withrajeev Says:

    [+]

    Hi, This is a great option, but i would like to understand how is QTP picking up the functions once it is added to the registry... ...

  13. babyou1st Says:

    [-]

    Hats oFF……

  14. usa77 Says:

    [-]

    Really its Excellent.. Its a greate learning.

  15. rajivkumarnandvani Says:

    [+]

    great man its works i create for excel com object but when i run this its runs only one time after that it gives an error " The ... ...

  16. rajivkumarnandvani Says:

    [+]

    Sorry it working for excel object but give error for word object see the below '' ObjMsWord i created in registory for word obje... ...

  17. Anil Says:

    [-]

    Its very much useful. Good job.

  18. getnoor4u Says:

    [-]

    very good article.

Leave a Reply

You must be logged in to post a comment.

This article was viewed 2038 times