Facebook

Course Name Start Date Time Duration Registration Link
No Training Programs Scheduled ClickHere to Contact
Please mail To sudhakar@qtpsudhakar.com to Register for any training

Thursday, February 12, 2009

Using Dictionary Objects In QTP

                                                Dictionary Object

Arrays are the first construct that VBScript instructors introduce when they discuss how to group data. With arrays, you can store columns of data in one place, then access the data later through one variable. However, years of real-world use have revealed that arrays aren't always the most desirable solution to gather and maintain related data. Fortunately, a new type of array has emerged: the dictionary. Here's a look at what dictionaries are and how you manipulate them with the methods and properties.

The Dictionary Object's Methods and Properties
Method or Property        Description
Methods
AddAdds a new item to the dictionary
ExistsVerifies whether a given key exists in the dictionary
ItemsReturns an array with all the values in a dictionary
KeysReturns an array with all the keys in a dictionary
RemoveRemoves the item identified by the specified key
RemoveAllRemoves all the items in the dictionary
Properties
CountReturns the number of items in a dictionary
ItemSets and returns an item for the specified key
KeyChanges an item's key

'***************************************************************
 
'Creation of a Dictionary Object
 
Dim d   ' Create a variable.
 
Set d = CreateObject("Scripting.Dictionary")
 
Here 'd' is a variable which is converted into a dictionary object.
 
'*************************************************************** 
'***************************************************************
 
'Adding items to Dictionary Object
 
Syntax:
 
Object.Add Item,Value
 
Dim d   ' Create a variable.
 
Set d = CreateObject("Scripting.Dictionary")
 
d.Add "a", "Athens"   ' Add some keys and items.
 
d.Add "b", "Belgrade"
 
d.Add "c", "Cairo"
 
'*************************************************************** 
'***************************************************************
 
'To check for Key Exist or not
 
Dim d   ' Create a variable.
 
Set d = CreateObject("Scripting.Dictionary")
 
d.Add "a", "Athens"   ' Add some keys and items.
 
d.Add "b", "Belgrade"
 
d.Add "c", "Cairo"
 
If d.Exists("c") Then
 
msgbox "key exists"
Else
 
msgbox "key doesn't exist"
End If
 
'***************************************************************
'***************************************************************
 
'To get values of Items 
 
Dim a, d, i, iList
 
Set d = CreateObject("Scripting.Dictionary")
 
d.Add "a", "Athens"
 
d.Add "b", "Belgrade"
 
d.Add "c", "Cairo"
 
iList = d.Items 
 
For i = 0 To d.Count -1
 
msgbox iList(i)
 
Next
 
'***************************************************************
'***************************************************************
 
'To get names of the Keys 
 
Dim a, d, i, iList
 
Set d = CreateObject("Scripting.Dictionary")
 
d.Add "a", "Athens"
 
d.Add "b", "Belgrade"
 
d.Add "c", "Cairo"
 
iList = d.Keys 
 
For i = 0 To d.Count -1
 
msgbox iList(i)
 
Next
 
'***************************************************************
'*************************************************************** 
'To Remove a key from Dictionary Object 
 
Dim a, d
 
Set d = CreateObject("Scripting.Dictionary")
 
d.Add "a", "Athens"
 
d.Add "b", "Belgrade"
 
d.Add "c", "Cairo" 
 
d.Remove("b")   ' Remove second pair.
 
'*************************************************************** 
'***************************************************************
 
'To Remove all keys from Dictionary 
 
Dim d
 
Set d = CreateObject("Scripting.Dictionary")
 
d.Add "a", "Athens"
 
d.Add "b", "Belgrade"
 
d.Add "c", "Cairo" 
 
d.RemoveAll   ' Clear the dictionary.
 
'*************************************************************** 
'***************************************************************
 
'To get value of Single Key 
 
Dim d
 
Set d = CreateObject("Scripting.Dictionary")
 
d.Add "a", "Athens"
 
d.Add "b", "Belgrade"
 
d.Add "c", "Cairo" 
 
msgbox d("a") or msgbox d.Item("a")
 
'*************************************************************** 
'***************************************************************
'Using Dictionary object in functions 
 
Set UDetails=CreateObject("Scripting.Dictionary")
 
UDetails.add "UserName","Sudhakar"
 
UDetails.add "Password","qtp" 
 
'Here is a dictionary object with user name and password. 
'To use these details in a function we should develop functions in this format.
 
Function Login(UserDetails)
 
Browser(bName).Page(pName).webedit(uName).set  UserDetails("UserName")
 
Browser(bName).Page(pName).webedit(pwd).set  UserDetails("Password")
 
Browser(bName).Page(pName).webbutton(bName).click
End Function
 
Calling the created function
 
call Login (Udetails)
 
'*************************************************************** 
'*************************************************************** 

11 comments :

  1. really good one, very useful.
    Thanks a lot.

    Where we store the defined Dictionary objects, is it in function library or somewhere else?

    Vishnu

    ReplyDelete
  2. Nice Article.Very Useful.

    Thanks for sharing!!!!!!

    ReplyDelete
  3. its good for every one...thanks a lot

    ReplyDelete
  4. very useful article.

    ReplyDelete
  5. how can we use Dictionary objects in multiple actions

    ReplyDelete
  6. Hi,
    I am new to QTP. Trying to automate WebTable which scenarios.

    I have to add, modify and delete rows in WT. Every time I make a modification. I will re construct the object to get correct row count.

    My problem is after certain iternations. WT object is not recognised. I am on QTP11/IE8. Any insight will be greatly apprecited.

    Thanx,

    ReplyDelete
  7. Thanks a lot. So nicely explained

    ReplyDelete