Wednesday, 22 April 2015

QTP - 5- Why we use dictionary object in QTP inspite we have a good option of storing data in arrays?

This could be a tricky question by an interviewer. Well lemme take you through the dictionary object and uses. As arrays, the dictionary functionality in QTP is also used to store values , but the difference comes out to be in the datatype. As we know we can only store similar type of data in an array so only same type of object can be stored in arrays on the contrary the QTP has given the flexibility of storing values in key value pair in dictionary. We can store any type of data against any key value in dictionary object.The key value should be unique against each value. One more benefit I can talk about is the indexing, in array we have predefined indexes while in QTP we can give indexes or keys as per our requirement.Its very simple to create a dictionary object and add any values :

> Set Objname = createobject("scripting.ditionary")

Now if we want to add any key value pair to the dictionary we can do as :

> Objname.add "Key","Value"

NOTE : for string datatype we need to put a double quotes "" , but for integer we don't need it.
Similarly we can define multiple dictionary objects.

If we want to count the key and values we can simply use the following command.

> Objname.Count

If we want to see any value against any key we can use the following command :

>objname.item("key name")

If we want to set any new value then :

>Objname.Item("Key Name")= "Key value"

If we want to update any key name

> Objectname.key("key name")= "New Key name"

If we want to get the name of all the keys/items in the dictionary

> arr = Objectname.Keys
> arr =Objectname.Items

further we can apply array Ubound and Lbound methods to display the keys.

If we want to remove any items we can use the following command :
> Objectname.Remove("Key")

and if we want to remove all items from dictionary
>Objectname.RemoveAll

and lastly if we want to check if any item exist or not then we can simply use
> Objectname.Exist("Key")

These are few basic dictionary commands.

No comments:

Post a Comment