Wednesday, November 16, 2011

Work With Dictionary Objects

Dictionary Object


Dictionary Object stores data key, item pairs. A Dictionary object stores the items in the array. Each item is associated with a unique key. The key is used to retrieve an individual item and is usually an integer or a string, but can be anything except an array.

Adavntages of using it in QTP:
1. can be used as Global variable declaration. so that any test can access the values from it in the run time.
2. You can store and retrive any number of run time values in to dictonary.
3. It is one of the Parameterization technique we can use in QTP

Disadvantages:
we can not specify the values in the desingn time like Datatable , Action parameters, environment variable. Useful in RunTime only.

Add Method

Adds a key and item pair to a Dictionary

object. object.Add (key, item)

Arguments : object Required. Always the name of a Dictionary object.
key Required. The key associated with the item being added.item Required.
The item associated with the key being added.

Remarks
An error occurs if the key already exists.

Example - Add Method

Dim d ‘ Create a variable.
Set d = CreateObject(“Scripting.Dictionary”)
d.Add “a”, “Athens” ‘ Add some keys and items.
d.Add “b”, “Belgrade”

Items Method

Returns an array containing all the items in a Dictionary object.

object.Items( )
Remarks The object is always the name of a Dictionary object.The following code illustrates use of the Items method:

Set d = CreateObject(“Scripting.Dictionary”)
d.Add “a”, “Athens” ‘ Add some keys and items.
d.Add “b”, “Belgrade”
a = d.Items ‘ Get the items.
For i = 0 To d.Count -1 ‘ Iterate the array.
s = s & a(i) & “
” ‘ Create return string.
Next
Msgbox s
Exists Method

Returns true if a specified key exists in the Dictionary object, false if it does not.

object.Exists(key)

Arguments

object Required. Always the name of a Dictionary object.
key Required. Key value being searched for in the Dictionary object.

Remarks

The following example illustrates the use of the Exists method.

Set d = CreateObject(“Scripting.Dictionary”)
d.Add “a”, “Athens” ‘ Add some keys and items.
d.Add “b”, “Belgrade”
If d.Exists(“c”) Then
Msgbox “Specified key exists.”
Else
Msgbox “Specified key doesn’t exist.”
End If
Keys Method

Returns an array containing all existing keys in a Dictionary object.

object.Keys( )

Remarks

The object is always the name of a Dictionary object.

The following code illustrates use of the Keys method:

Dim a, d, i ‘ Create some variables.
Set d = CreateObject(“Scripting.Dictionary”)
d.Add “a”, “Athens” ‘ Add some keys and items.
d.Add “b”, “Belgrade”
a = d.Keys ‘ Get the keys.
For i = 0 To d.Count -1 ‘ Iterate the array.
s = s & a(i) & “
” ‘ Return results.
Next
Msgbox s
Remove Method

Removes a key, item pair from a Dictionary object.

object.Remove(key)

Arguments
object Required. Always the name of a Dictionary object.
key Required. Key associated with the key, item pair you want to remove from the Dictionary object.

Remarks

An error occurs if the specified key, item pair does not exist.
The following code illustrates use of the Remove method:

Dim a, d ‘ Create some variables.
Set d = CreateObject(“Scripting.Dictionary”)
d.Add “a”, “Athens” ‘ Add some keys and items.
d.Add “b”, “Belgrade”
d.Add “c”, “Cairo” …
d.Remove(“b”) ‘ Remove second pair.
RemoveAll Method

The RemoveAll method removes all key, item pairs from a Dictionary object.

object.RemoveAll( )

Dim a, d, i ‘ Create some variables.
Set d = CreateObject(“Scripting.Dictionary”)
d.Add “a”, “Athens” ‘ Add some keys and items.
d.Add “b”, “Belgrade”
d.Add “c”, “Cairo” …
a = d.RemoveAll ‘ Clear the dictionary.

1 comment:

  1. Selenium is an open source automated testing tool used for testing web applications. Training on selenium help students to be aspiring testing professionals in leading software development firm.
    Selenium Course in Chennai

    ReplyDelete