Monday, December 8, 2008

QTP Dictionary Objects

Add values to a Dictionary object in one action and retrieve the values in other actions.

The Dictionary object enables you to assign values to variables that are accessible from all actions (local and external) called in the test in which the Dictionary object is created.

Dictionary Objects have methods like Add (Adds a key and item pair to a Dictionary object), Items (Returns an array containing all the items in a Dictionary object.), Keys (Returns an array containing all existing keys in a Dictionary object.) etc. and properties like Count (Returns the number of items in a Dictionary object.), Item (Sets or returns an item for a specified key in a Dictionary object.). Below examples will make Dictionary Objects more clear. For complete information on these, see QTP User Guide.

To use the Dictionary object, you must first add a reserved object to the registry by following the below mentioned steps:

Go to Start-> Run. Run dialog opens.
Type regedit.exe and click ok. Registory Editor opens.
Go to HKEY_CURRENT_USER\Software\Mercury Interactive\QuickTest Professional\MicTest\ReservedObjects\
Under ReservedObjects create a new Key (Highlight ReservedObjects, go to Edit menu->New ->Key, and name it Dictionary. Right click on DIctionary, go to New-> String Value, Type the name as ProgID. Double click on this ProgID, Edit String dialog opens, type the Value data as Scripting.Dictionary and click ok)
Restart QuickTest Professional

A simple example showing Actions sharing information with the use of Dictionary Objects.

Open a new test in QTP.
In the Expert View of Action1 type:
Dictionary.Add "A", 123

Go to Insert->Call to New Action, to create Action2

In the Expert View of Action2 type:
msgbox dictionary("A")

Run the test.

Above you are setting the values in Action1 and accessing them in Action2.

Another simple example which shows that functions can return dictionary objects:

Dim dict_obj,i
Set dict_obj = CreateObject("Scripting.Dictionary")

set x=sum
msgbox x.item("three")

Public Function sum()
dict_obj.add "one",1
dict_obj.add "two",2

dict_obj.add "three",dict_obj("one") + dict_obj("two")
Set sum = dict_obj
End function