Thursday, June 3, 2010

Mercury KB3 - Usage of Dictionary Objects

Problem Description: How to use the Dictionary object
The Dictionary object can be used as an alternative to using environment variables when sharing values between Actions. The Dictionary object allows the user to assign values to variables that are accessible from all Actions (local and external) in the test in which the Dictionary object is created.
________________________________________
Solution: Create a Dictionary object to access the Dictionary methods
Use CreateObject to create an object reference to the Dictionary object ("Scripting.Dictionary"), then call the methods as needed.
Dictionary Object Methods:
• To add a value to a Dictionary object use the Add method.
object.Add(key, item)
object The name of the Dictionary object.
key The key associated with the item being added. Similiar to an index of an array.
item The item associated with the key being added. In other words, the value being assigned to the key.
In the following example, the Dictionary object is named v. The string (item) "Hello" is assigned to the key "value1."
Example:
Set v = CreateObject("Scripting.Dictionary")
v.Add "value1", "Hello"
• To retrieve an item(value) from a Dictionary object use the Item method.
object.Item(key)
object The name of the Dictionary object.
key The key of the value that is retrieved.
In the following example, the Item method is used to view the item (value) associated with the key named "value2" of the Dictionary object v.
Example:
Set v = CreateObject("Scripting.Dictionary")
v.Add "value1", "Hello"
msgbox v.Item("value1")
• To retrieve a collection of all the items in a Dictionary object and place into an array, use the Items method.
Note:
The previous Item method will retrieve one item. The Items method will retrieve a collection of items.
object.Items
object The name of the Dictionary object.
In the following example, the Items method will return all the items in Dictionary object v and place them in an array named myArray. The items(values) can be viewed by iterating through the array in the For-Next loop.
Example:
Set v = CreateObject("Scripting.Dictionary")
v.Add "value1", "Hello"
v.Add "value2", "Goodbye"
v.Add "status", "PASS"
myArray = v.Items
For i=0 to v.Count - 1
msgbox myArray(i)
Next
• To verify if a key of a Dictionary object exists, use the Exists method.
object.Exists(key)
object The name of the Dictionary object.
key The key value for which you are searching.
In the following example, the Exists method is used to verify if the key "value3" exists in the "v" Dictionary object.
Example:
Set v = CreateObject("Scripting.Dictionary")
v.Add "value1", "Hello"
v.Add "value2", "Goodbye"
v.Add "status", "PASS"
If v.Exists("value3") Then
msgbox "The key exists"
Else
msgbox "The key does not exist"
End If
• To return an array containing all of the keys of a Dictionary object, use the Keys method.
object.Keys
object The name of the Dictionary object.
In the following example, the Keys method will return all the keys in Dictionary object v and place them in an array named myArray. The keys can be viewed by iterating through the array in the For-Next loop.
Example:
Set v = CreateObject("Scripting.Dictionary")
v.Add "value1", "Hello"
v.Add "value2", "Goodbye"
v.Add "status", "PASS"
myArray = v.Keys
For i=0 to v.Count - 1
msgbox myArray(i)
Next
• To remove a key and its associated item from a Dictionary object, use the Remove method.
object.Remove(key)
object The name of the Dictionary object.
key The name of the key that will be removed from the Dictionary object.
In the following example, the "status" key and its associated value, "PASS," will be removed from the "v" Dictionary object.
Example:
Set v = CreateObject("Scripting.Dictionary")
v.Add "value1", "Hello"
v.Add "value2", "Goodbye"
v.Add "status", "PASS"
v.Remove("status")
• To remove all of the keys and associated items from a Dictionary object, use the RemoveAll method.
object.RemoveAll
object The name of the Dictionary object.
In the following example, all of the keys and associated items will be removed from the "v" Dictionary object.
Example:
Set v = CreateObject("Scripting.Dictionary")
v.Add "value1", "Hello"
v.Add "value2", "Goodbye"
v.Add "status", "PASS"
v.RemoveAll

You can also create a reserved test object for the Dictionary object. This will also allow you to share data between actions in a test. To do this, you must add it to the reserved object section of the registry.
1. Open Window's registry by opening a Run window and entering regedit.
2. Navigate to HKEY_CURRENT_USER\Software\Mercury Interactive\QuickTest Professional\MicTest\ReservedObjects.
3. Create a new key (folder) named GlobalDictionary by right-clicking on the ReservedObjects key and selecting New -> Key.
4. Under the new key, create a String value named ProgID by right-clicking on GlobalDictionary and selecting New -> String Value.
5. Assign "Scripting.Dictionary" to the new ProgID string value by right-clicking on ProgID and selecting "Modify."
Example:
GlobalDictionary.Add "a", "Athens"
msgbox GlobalDictionary.Exists("a")
GlobalDictionary.Remove("a")

No comments: