Dictionary Objects
Dictionary Objects are the perl arrays. In arrays we store the values based upon the index i.e. 0,1,2 but here we can store the values based upon the names i.e. keys. Each value is stored corresponding to the unique keys. Dictionary object that stores data in key, item pairs.
Dictionary objects are very useful when dealing with the large no of data. On an average they are 3 times faster then the collection objects
Every key should be unique. Duplicate keys are not allowed. Dictionary object belong to "Scripting.Dictionary" class. To create a dictionary object we can use the following statement:
Dim objDict
Set objDict=CreateObject("Scripting.Dictionary")
Following are the methods available with the Dictionary Objects:
Add Method (Dictionary): This method is used to add key;item pairs in the dictionary object
Example:
Dim strKey
For strKey=1 to 10
objDict.Add "key" & strKey, "value" & strKey
Next
Exists: This method is used to check if the specified key exists in the dictionary object or not
Exapmle:
objDict.Exists "key1"
objDict.Exists "Dictionary1"
Items Method; This method will return the array of items in a dictionary object. This is helpful in case we have to go through the all the values captured in the dictionary object dynamically.
Example:
Dim strItems
strItems=objDict.Items
Keys Method: Same as the above method. It will return the array of keys instead of items
Example:
Dim strKeys
strKeys=objDict.Keys
Remove Method: Remove a key item pair from the dictionary object
Example:
objDict.Remove("key1")
RemoveAll Method: it removes all the key item pair from the dictionary object
Properties:
Compare Mode: It sets or return the comparison mode for comparing string keys in a Dictionary object.
Example: objDict.CompareMode = 1
Count Property: it will return the no of items in the dictionary object
Example: print objDict.Count
Item Property: it returns or sets the new item for the specified key.
Example: print objDict.Item("key2") ' will print value2
Key Property: this is used to change specified key with the new key
objDict.Key("key2")="QTP"
print objDict.Item("QTP") ' will print value2
A hidden property of dictionary object: Hashval
Lets first understand the concept of Hashing
Another interesting thing with the dictionay objects is that we can use them to pass values between the different actions:
But if use it directly then it will give a error message that dictionary object is require.
We need to define the global dictionary objects :For that we need to create a entry in the registry and assign its ProgID the value=Scripting.Dictionary
or you can use the following code:
Dim WshShell
Set WshShell =CreateObject("WScript.Shell")
WshShell.RegWrite "HKCU\Software\Mercury Interactive\QuickTest Professional\MicTest\ReservedObjects\GlobalDictionary\ProgID", "Scripting.Dictionary","REG_SZ"
Set WshShell = Nothing
Dictionary objects are very useful when dealing with the large no of data. On an average they are 3 times faster then the collection objects
Every key should be unique. Duplicate keys are not allowed. Dictionary object belong to "Scripting.Dictionary" class. To create a dictionary object we can use the following statement:
Dim objDict
Set objDict=CreateObject("Scripting.Dictionary")
Following are the methods available with the Dictionary Objects:
Add Method (Dictionary): This method is used to add key;item pairs in the dictionary object
Example:
Dim strKey
For strKey=1 to 10
objDict.Add "key" & strKey, "value" & strKey
Next
Exists: This method is used to check if the specified key exists in the dictionary object or not
Exapmle:
objDict.Exists "key1"
objDict.Exists "Dictionary1"
Items Method; This method will return the array of items in a dictionary object. This is helpful in case we have to go through the all the values captured in the dictionary object dynamically.
Example:
Dim strItems
strItems=objDict.Items
Keys Method: Same as the above method. It will return the array of keys instead of items
Example:
Dim strKeys
strKeys=objDict.Keys
Remove Method: Remove a key item pair from the dictionary object
Example:
objDict.Remove("key1")
RemoveAll Method: it removes all the key item pair from the dictionary object
Properties:
Compare Mode: It sets or return the comparison mode for comparing string keys in a Dictionary object.
Example: objDict.CompareMode = 1
Count Property: it will return the no of items in the dictionary object
Example: print objDict.Count
Item Property: it returns or sets the new item for the specified key.
Example: print objDict.Item("key2") ' will print value2
Key Property: this is used to change specified key with the new key
objDict.Key("key2")="QTP"
print objDict.Item("QTP") ' will print value2
A hidden property of dictionary object: Hashval
Lets first understand the concept of Hashing
A hash (or 'hash value') is a number generated from a string of text. The string could have numbers in it, but for hashing, consider them to be just text characters. The hash value is calculated so that it's unlikely - but not impossible - for some other text string to result in the same hash value. But the calculation guarantees that the same string will always produce the same hash value.
A hash value is usually seen in security programs where it's necessary to make sure that some string, such a transmitted message, hasn't been changed. The usual process is that the sender will create a hash of the message text string and then encrypt the hash. Both the encrypted hash and the string are sent to a recipient. The recipient then decrypts the hash, produces another hash using the same hashing algorithm from the received message, and compares the two hashes. If they're the same, the message is considered to be exactly the same as the one transmitted.
So we can also use generate the hashval
Example:
print objDict.hashval("Water")
print objDict.hashval("Earth")
This can be useful in some scenarios like comparing arrays:
Example:
Dim i
Dim arr1()
For i=1 to 10
ReDim preserve arr1(i-1)
arr1(i-1)=i
Next
Dim j
Dim arr2()
For i=1 to 10
ReDim preserve arr2(i-1)
arr2(i-1)=i
Next
If objDict.HashVal(join(arr1))=objDict.HashVal(join(arr2)) Then
print "Arrays are equal"
End If
Another interesting thing with the dictionay objects is that we can use them to pass values between the different actions:
But if use it directly then it will give a error message that dictionary object is require.
We need to define the global dictionary objects :
or you can use the following code:
Dim WshShell
Set WshShell =CreateObject("WScript.Shell")
WshShell.RegWrite "HKCU\Software\Mercury Interactive\QuickTest Professional\MicTest\ReservedObjects\GlobalDictionary\ProgID", "Scripting.Dictionary","REG_SZ"
Set WshShell = Nothing
Now Global Dictionary object is ready and you can use it with any action in ur script. Moreover now you need not to define it.
Excellent explanation with realtime suitable scripting examples about Global Dictionary Objects. Thanks.
ReplyDeleteThis is an awesome article. Keep it going....
ReplyDelete-P
I am using a BPT framework and qtp scripts are run from QC. I am using the concept of Global dictionary by making the registry settings to avoild repeated database calls and want to hold the items in a global dictionary. The issue i am facing is when the 2nd action gets called the Global Dictionary loses its contents. How can i persist the contents in the Global Dictionary to be used across all actions
ReplyDeletecan u check that the entry exists in the registry ?
ReplyDeleteUnfortunately observed that the Global dicitonary contents get flushed out when the 2nd component loads..This is typicaly observed in an HP-BPT framework dont know of a suitable workaround for the same
ReplyDeleteShreejit
This comment has been removed by the author.
ReplyDeleteThanks a lot for good Stuff.
ReplyDeleteVery Very Helpful Data.
Hi Ashish,
ReplyDeleteThe Article was very useful.
Can you please elaborate more on the compare mode by giving a real time example as you have given it for others.
Keep blogging such articles.
Excellent narration..thank you so much for this wonderful explanation.
ReplyDelete