Using the Dictionary Object

As an alternative to using environment variables to share values between actions, you can use the Dictionary object. The Dictionary object enables you to assign values to variables that are accessible from all actions (local and external) called in the test in which the Dictionary object is created.

You can view sample files that use Dictionary syntax in the <Installdir>\CodeSamplesPlus folder, including the DictionarySample.py file.

VBScript sample

Copy code
'In order to have IntelliSense for the Dictionary object, and have it recognized by other actions, it is added to the registry
Dim WshShell
Set WshShell =CreateObject("WScript.Shell")
WshShell.RegWrite "HKCU\Software\Mercury Interactive\QuickTest Professional\MicTest\ReservedObjects\GlobalDictionary\ProgID", "Scripting.Dictionary","REG_SZ"
Set WshShell = Nothing

' After updating the registry, you must close and reopen OpenText Functional Testing.
' ***********************************************************************************

' Available methods
' -----------------------------
' Exists
    GlobalDictionary.Exists(<Key Name>) ' Returns True or False

' Remove
    GlobalDictionary.Remove(<Key Name>) ' Remove a specific key

' RemoveAll
    GlobalDictionary.RemoveAll ' Removes all keys

' Add
    GlobalDictionary.Add <Key Name>, <Value> ' Create a new key and assigns its value

' Item
    GlobalDictionary.Item(<Key Name>) ' Gets/Sets a key value

Python sample

Copy code
class GlobalDictionary:
    """
    Globally accessible dictionary object.
    """

    def __init__(self):
        self._data = {}

    def exists(self, key):
        return key in self._data

    def add(self, key, value):
        self._data[key] = value

    def remove(self, key):
        self._data.pop(key, None)

    def remove_all(self):
        self._data.clear()

    def item(self, key):
        return self._data.get(key)

    def set_item(self, key, value):
        self._data[key] = value


GlobalDictionary = GlobalDictionary()
GlobalDictionary.add("User", "John")
GlobalDictionary.add("Role", "QA")

if GlobalDictionary.exists("User"):
    print(GlobalDictionary.item("User"))

GlobalDictionary.remove("Role")