Thursday 8 May 2014

QTP - UFT - How to create a Dictionary object using VBScript or how to work with Dictionary object in QTP or UFT


Overview
Dictionary Object  stores data key, item pairs. A Dictionary object stores the items 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 of using it in UFT/QTP:Can be used as Global variable declaration. so that any test can access the values from it in the run time.
You can store and retrieve any number of run time values in to dictionary.
It is one of the Parameterization technique we can use in QTP
Disadvantages:we can not specify the values in the design time like Datatable , Action parameters, environment variable. So it is useful only in Run time , not design time

Code
'Sample code to retrieve all Keys of dictionary. replace the keyword Keys with Items to get all the list of items in dictionary
Set o1 = createobject("Scripting.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"
arrDict = o1.Keys
For i = 0 To Ubound(arrDict)
    msgbox arrDict(i)
Next


'Some time you may require to get the Key and Item pair
Set o1 = createobject("Scripting.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"
arrDict = o1.Keys
For i = 0 To Ubound(arrDict)
    msgbox "Key - '" & arrDict(i) & "' Item - '" & o1.Item(arrDict(i)) & "'"
Next

Overview of method and properties that can be handy

'To Create a dictionary - Scripting.Dictionary object should be created
'Set objDictionaryName = Createobject("Scripting.Dictionary")

' Syntax 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.count

' The below code removes the key and its items from the dictionary
'objDictionaryName.Remove(key)

' The below code removes all the keys and Items available in dictionary
'objDictionaryName.RemoveAll

' The below code returns you the array collection of Keys available in Dictionary
'objDictionaryName.Keys

' The below code 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
' vbBinaryCompare - in this mode text case sensitivity will be taken into consideration
'objDictionaryName.CompareMode = vbTextCompare

' here are some examples
strKey = "R2014010175489"
strItemVal = "Mike Tuffer"
Set objDict = Createobject("Scripting.Dictionary")
objDict.Add strKey,strItemVal

'result - outputItemVal 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


' Below code 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

' The below code returns you the array collection of Keys available in Dictionary
objDict.Keys

' The below code removes the "R2014010175489" and its item "Mike Tuffer" from the dictionary
objDict.Remove(strKey)

' The below code removes all the keys and Items available in dictionary. This is used in Clearing the dictionary items if it has multiple Key, Items pairs
objDict.RemoveAll

' The below code will perform vbtextcomparasion on the Key
objDict.CompareMode = vbTextCompare


9 comments: