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?
Set ObjDic=CreateObject(“Scripting.Dictionary”)
To add items to dictionary object use the Add method
1 2 3 4 |
ObjDic.Add "Name", "John" ObjDic.Add "Age", 30 ObjDic.Add "City", "New Delhi" ObjDic.Add "Country", "India" |
To get a required value from the dictionary object use the Item method and provide the Key name.
1 |
Msgbox ObjDic.Item("Age") |
The output will be 30.
Dictionary Object Methods in UFT
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.
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.
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.
1 2 3 4 5 6 7 8 9 10 11 12 |
Set ObjDic= Createobject("Scripting.Dictionary") ObjDic.Add "AddLine1", "TEST ADDRESS LINE 1" ObjDic.Add "AddLine2", "TEST ADDRESS LINE 2" ObjDic.Add "AddLine3", "TEST ADDRESS LINE 3" ObjDic.Add "PinCode", "110025" Call UpdateAddress(ObjDic) Function UpdateAddress(DicObjAddress) Msgbox DicObjAddress.Item("AddLine1") 'Simalarly get values from all required Keys End Function |
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
- Amazing Facts about Object Repository Manager in UFT
- How to Use Insight Object in UFT
- Action Input And Output Parameters In UFT
- What are new features in the new UFT One Datatable
- Component Object Model in UFT | DOM | TOM & BOM
- Automation Object Model in UFT One (AOM)
- How to Use LoadAndRunAction in UFT
- Difference between ChildObjects and ChildItem in UFT
- VBScript Loops in UFT
- VBScript MySQL Database Connection in UFT