A
Dictionary object stores the items (data key, item pairs) in the array. Each
item is associated with a unique key. The key is used to retrieve an individual
item and is usually an integer or a string, but can be anything except an
array.
Advantages: it Can be
used as “Global variable” declaration, so that any test can access the values
from it in the run time. We can store and retrieve any number of run time values
from dictionary.
It
is one of the Parameterization techniques we can use in QTP/UFT.
Disadvantages: we can
not specify the values in the design time like Data table , Action parameters,
environment variable, So it is useful only in Run time , not design time
Methods of dictionary object:
1. Add Method - Adds
a key and item pair to a Dictionary object.
odict.add "Currency", "Rupee"
2. Exists Method -
Returns true if a specified key exists in the Dictionary object, false if it
does not.
odict.exists(“country”) ‘will
return true for dictionary defined in
above examples.
3. Items Method -
Returns an array containing all the items in a Dictionary
object.aite = odict.items(“country”)
4. Keys Method -
Returns an array containing all existing keys in a Dictionary
object.aite = odict.keys(1)
5. Remove Method -
Removes a key, item pair from a Dictionary
object.odict.remove("Currency")
6. RemoveAll Method - The RemoveAll method removes all key,
item pairs from a Dictionary
object.odict.removeAll()
Properties of Dictionary object:
1. Count Property - Returns the number of items in a
collection or Dictionary.
object.Intcount = odict.count
2. Item Property - Sets or returns an item for a specified
key in a Dictionary object. For collections, returns an item based on the
specified
key.ItemVal =
odict.Item("Capital") ‘ gets
value of the key
odict.Item("Capital") = “MUMBAI” ‘ sets value of the key.
3. Key Property - Sets a key in a Dictionary
object.odict.Key("Capital") = "City"
odict.item(“City”) ‘will
now give value of item stored in key Capital before.
Overview:
'To Create a dictionary ,Scripting.Dictionary object should be
created
Set
objDictionaryName = Createobject("Scripting.Dictionary")
'To add a dictionary item
objDictionaryName.Add(Key,Item)
'To Retrieve an Item
objDictionaryName.Item(key)
'To retrieve the count of items in dictionary
objDictionaryName.count
'To check if a Key exists in a Dictionary
objDictionaryName.Key
‘Removes the key and its items from the dictionary
objDictionaryName.Remove(key)
'Removes all the keys and Items available in dictionary
objDictionaryName.RemoveAll
'Returns the array collection of Keys available in
Dictionary
objDictionaryName.Keys
'Performs the type of comparison needed on the key
They
are two types of comparison modes –
1)vbTextCompare - in this mode text case
sensitivity is ignored
2)vbBinaryCompare - in this mode text case
sensitivity will be taken into consideration.
objDictionaryName.CompareMode =
vbTextCompare
Example-1:
Simple
Script to retrieve all Keys of dictionary and replace the keyword Keys with
Items to get all the list of items in dictionary.
‘Create Dictionary Object
Set
o1 = createobject("Scripting.dictionary")
‘Add Object to Dictionary
o1.Add
"QTP","Fucntional Testing Tool"
o1.Add
"Load Runner","Performance Testing tool"
o1.Add
"Service Test","Speciality in WebSerices"
o1.Add
"Sprinter","ALM Connection needed"
‘To get Array
arrDict
= o1.Keys
For
i = 0 To Ubound(arrDict)
msgbox arrDict(i)
Next
‘To get the Key and Item pair
For
i = 0 To Ubound(arrDict)
msgbox "Key - '" & arrDict(i)
& "' Item - '" & o1.Item(arrDict(i)) & "'"
Next
Example-2:
strKey
= "R2014010175489"
strItemVal
= "Mike Tuffer"
Set
objDict = Createobject("Scripting.Dictionary")
objDict.Add
strKey,strItemVal
'result - output ItemVal contain the "Mike
Tuffer"
outputItemVal
= objDict.Item(strKey)
print
outputItemVal
'Result - intDictItemsCount contains the number of
items already available in Dictionary in the current case it is "1"
intDictItemsCount
= objDict.Count
'Checks if key is
available in the dictionary
If
objDict.Exists(strKey) then
reporter.ReportEvent micPass,"Check
Dictionaryfor key " & strKey,"Dictionary contains the key "
& strKey
else
reporter.ReportEvent micFail,"Check
Dictionaryfor key " & strKey,"Dictionary does not contains the
key " & strKey
End
if
‘Returns you the array collection of Keys available in
Dictionary
objDict.Keys
'Removes the "R2014010175489" and its item "Mike
Tuffer" from the dictionary
objDict.Remove(strKey)
No comments:
Post a Comment
Note: only a member of this blog may post a comment.