Facebook

For any corporate training requirements Please mail To sudhakar@qtpsudhakar.com
New Schedule in MindQ Systems, Madhapur By SUDHAKAR
Course Name Date Time Duration Type Branch
Selenium+Appium 16th October 2017 7.30 AM - 9.00 AM IST 45 Days ClassRoom + Online Mind Q Madhapur
Selenium+Appium 30th October 2017 9.30 AM - 11.00 AM IST 45 Days ClassRoom + Online Mind Q Madhapur

A book "Cracking the QTP interview" authored by Sudhakar.Read More. Buy From FlipKart

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)
 
'*************************************************************** 
'*************************************************************** 

10 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