Friday, 21 October 2011

Dictionary Object In QTP

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.
Methods:

Add Method
Adds a key and item pair to a Dictionary
object. object.Add (key, item)
Arguments 
object Required. Always the name of a Dictionary object.
key Required. The key associated with the item being added.item Required.
The item associated with the key being added. 
Remarks
An error occurs if the key already exists.
The following example illustrates the use of the Add method.
Dim var1   ‘ Create a variable. 
Set var1 = CreateObject(“Scripting.Dictionary”) 
 var1.Add “a”, “value1”   ‘ Add some keys and items.  
var1.Add “b”, “value2”
Items Method
 Returns an array containing all the items in a Dictionary object. 
      object.Items( )
 Remarks : The object is always the name of a Dictionary object.The following code illustrates use of theItems method:
Set var1 = CreateObject(“Scripting.Dictionary”)   
 var1.Add “value1”, “NewValue1”   ‘ Add some keys and items.  
 var1.Add “value2”, “NewValue2”    
value1= var1.Items   ‘ Get the items.  
  For i = 0 To var1.Count -1 ‘ Iterate the array.
       stmt = stmt & value1 (i) & “<BR> ‘ Create return string.  
      Next
Msgbox stmt  
 Exists Method
 Returns true if a specified key exists in the Dictionary object, false if it does not.
object.Exists(key)
 Arguments
object Required. Always the name of a Dictionary object.
key Required. Key value being searched for in the Dictionary object.
 Remarks :
The following example illustrates the use of the Exists method.
Set var1 = CreateObject(“Scripting.Dictionary”)  
var1.Add “value1”, “Newvalue1”   ‘ Add some   keys and items.   
var1.Add “value2”, “Newvalue2”   
   If var1.Exists(“value3”) Then   
    Msgbox  “Specified key exists.”   
 Else      
 Msgbox  “Specified key doesn’t exist.” 
   End If
Keys Method
Returns an array containing all existing keys in a Dictionary object.
 object.Keys( )
Remarks
The object is always the name of a Dictionary object.
The following code illustrates use of the Keys method:   
Dim var1,var2,var3   ‘ Create some variables.   
 Set var2= CreateObject(“Scripting.Dictionary”)   
 var2.Add “var1”, “Newvalue1”   ‘ Add some keys and items.  
  var2.Add “var3”, “Newvalue2”   
  var1 = var2.Keys   ‘ Get the keys. 
   For i = 0 To d.Count -1 ‘ Iterate the array.    
   stmt = stmt & var1(i) & “<BR>‘ Return results.   
  Next
Msgbox stmt
Remove Method  
Removes a key, item pair from a Dictionary object. 
object.Remove(key) 
Arguments
 object Required. Always the name of a Dictionary object.
key Required. Key associated with the key, item pair you want to remove from the Dictionary object.
Remarks
An error occurs if the specified key, item pair does not exist.
The following code illustrates use of the Remove method:
Dim var1, var2   ‘ Create some variables. 
Set var2 = CreateObject(“Scripting.Dictionary”) 
var2.Add “var1”, “value1”   ‘ Add some keys and items. 
var2.Add “var3”, “value2”
 var2.Add “var4”, “value3” … 
var2.Remove(“var3”)   ‘ Remove second pair.
RemoveAll Method
The RemoveAll method removes all key, item pairs from a Dictionary object.
object.RemoveAll( ) 
Dim var2,var1,var2   ‘ Create some variables.
 Set var1 = CreateObject(“Scripting.Dictionary”) 
var1.Add “v1”, “value1”   ‘ Add some keys and items.
 var1.Add “v2”, “value2” 
var1.Add “v3”, “value3” … 
var2 = var1.RemoveAll   ‘ Clear the dictionary.

Advantages of using it in QTP:

1. can be used as Global variable declaration. so that any test can access the values from it in the run time.
2. You can store and retrieve any number of run time values in to dictonary.
3. It is one of the Parameterization technique we can use in QTP
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