How To Use Dictionary Object in UFT With Examples

A dictionary object in UFT is very much similar to an array. Unlike an array index, there is a unique key associated with every item of a dictionary object. Using the Key name, the associated value can be called anywhere in the script.

How to Create a Dictionary Object in UFT and Add Items to it?

To create a dictionary object, use the below syntax

Set ObjDic=CreateObject(“Scripting.Dictionary”)

To add items to dictionary object use the Add method

To get a required value from the dictionary object use the Item method and provide the Key name.

The output will be 30.

Dictionary Object Methods in UFT

The following are the available methods available under a dictionary object in UFT/QTP.
Dictionary object Methods
CompareMode: This method sets and returns the comparison mode for comparing string keys in a Dictionary object.

Frequently used comapre modes are vbBinaryCompare and vbTextCompare
ObjDic.CompareMode = vbBinaryCompare ‘Default compare mode
ObjDic.CompareMode = vbTextCompare

If vbTextCompare is on, you can not define more than one Key having the same names but of different cases.UFT will throw an error at runtime.

If vbBinaryCompare is on, you can define more than one Key having the same names but of different cases and UFT will not throw any error at runtime.

Example: Here Japan will be displayed in the message box because Key name Country with all characters in the lower case has been passed in the message box.

Count: Returns the number of items in a collection or Dictionary object.
Exists: It determines if a key exists in the dictionary object.
Item: It returns the value of the required key to the Dictionary object.
Dictionary Object item UFT
Items: It returns an array with all the values to the Dictionary object.
Dictionary Object items UFT
Key: It sets a new key name to an existing key in a Dictionary object.
Keys: It returns an array with all the available Keys to the Dictionary Object.
Remove: It removes a key, item pair from a Dictionary object.
RemoveAll: It method removes all key, item pairs from a Dictionary object.

When Dictionary objects should be used in UFT?

There are no hard and fast rules for using dictionary objects. It’s up to the choice of automation tester.I am going to show you a real example of using a dictionary object which is often used while developing an automation framework. Dictionary objects are very useful when you have an idea that the numbers of parameters of a function might change in the future. If a function has been called in 100 scripts and its parameters got changed later on, it would be a cumbersome task to update that calling the function in all those 100 scripts. So how to figure it out.  

Example: Let us suppose there is a function name UpdateAddress and initially its parameters are Address Line 1, Address Line 2, City, and PinCode.Later on, Address Line 3 was introduced in the functionality. Now you will have to add a new parameter to the UpdateAddress function. It will require huge maintenance. To avoid such a situation Dictionary object is the best solution to the problem.

So in the function UpdateAddress we are using only one parameter that is a Dictionary object. If the number of keys increases or decreases it does not affect the number of parameters of the function. Hence no maintenance would be required in any of the scripts in which the UpdateAddress function is called.

Recommended Posts

Leave a Reply