Blog to understand automation concepts in QTP, Selenium Webdriver and Manual Testing concepts

How to work with Dictionaries using scripting.dictionary object in QTP

Dictionary Object stores data key, item pairs. Each item is associated with a unique key.The key is used to retrieve an individual item and is usually a integer or a string, but can be anything except an array.

Creating a dictionary object:

We can create an instance of dictionary object as shown below:
Set odict = CreateObject("Scripting.Dictionary")
odict.add "country", "India"
odict.add "Capital", "Delhi"  

 
We have created a dictionary object odict, and added 2 key item collection in the dictionary object.
 

Below are the 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 keyodict.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. 


Comparing Dictionaries Vs Arrays


Below is the comparison between array and dictionary objects
  •  Dictionary  has methods to add new items and check for existing items.
  •  On Deleting a particular item from a dictionary, all the subsequent items automatically shift up.
  • You use keys to identify dictionary items. Keys can be any data type while arrays have numeric values as key
  •  A dictionary can't be multidimensional while array can be.
  •  It is much easier to find an item value based on keys as compared to arrays.
  •  Keys in Dictionary can have unique values only
  •  Dictionary can be used as Global variable declaration. so that any test can access the values from it in the run time. Will discuss this in detail in a separate post.

4 comments:

  1. Thanks.
    The Array returned by Exist method is single dimensional or Two ?

    ReplyDelete
    Replies
    1. Thanks friend, there was a typo issue. Corrected it.

      So exists method validates that the key exists in the dictionary and returns true or false based on the existence of it. So exist will not return an array but boolean value

      Delete
  2. your incrementer for visitor counter is wrong as it gets updated every time a page is loaded by a same user

    ReplyDelete