Wednesday, 2 April 2008

QTP: Debug class for Dictionary

As I mentioned earlier, I use the Dictionary object a lot in my test framework. However it requires some extra efforts when debugging in QTP compared to ordinary variables since your Keys and Items are not visable by default in Debug viewer's Variables tab. You either manually add Keys to the Watch tab or add extra code where your Dictionary Keys and Items are stored in an array:

arrKeys = objDictionary.Keys
arrValues = objDictionary.Items


The last couple of days I have tried to learn a bit about Classes in VBScript and I noticed that a class public variables are available in QTP's Debug Variables tab in alphabetic order. That made me come up with this crazy idea to maybe replace the use of Dictionary object with a custom class instead...but Classes in VBScript seems a bit limited according to my requirements. However, for debugging purposes it could be nice to create a "Class copy object" of a Dictionary:

'Create a dictionary object and add some parameters
Set objDictionary = CreateObject("Scripting.Dictionary")
objDictionary.Add "A_Parameter","47"
objDictionary.Add "Z_Parameter",""
objDictionary.Add "C_Parameter","11"
'Create a class out of the dictionary and a new debug object
Execute CreateClass("DebugDictionary", objDictionary)
Set objDictionaryDebug = New DebugDictionary
'Display A_Parameter value
Msgbox objDictionaryDebug.C_Parameter
Set objDictionaryDebug = Nothing
Set objDictionary = Nothing


Function CreateClass (strClassName, objDictionary)
'* Create Class string from Dictionary object
'* By Stefan Thelenius 2008-04-02
'* Usage example: Execute CreateClass("MyClass", objDictionary)
'Get keys and values into arrays
arrKeys = objDictionary.Keys
arrValues = objDictionary.Items
For i = 0 to UBound(arrKeys)
'Create Public variables
strClassPublicVar = strClassPublicVar & "Public " & arrKeys(i) & vbLf
'Set default values to variables
strClassPublicInit = strClassPublicInit & arrKeys(i) & "=""" & arrValues(i) & """" & vbLf
Next
'Add number of Keys as Count Int
strClassPublicVar = strClassPublicVar & "Public Count" & vbLf
strClassPublicInit = strClassPublicInit & "Count=" & UBound(arrKeys)+1 & vbLf
'Set Class and initializer
strClassStart = "Class " & strClassName & vbLf
strClassEnd = "End Class"
strClassInitStart = "Private Sub Class_Initialize" & vbLf
strClassInitEnd = "End Sub" & vbLf
'Return class string
CreateClass = strClassStart & strClassPublicVar & strClassInitStart & strClassPublicInit & _
strClassInitEnd & strClassEnd
End Function

Above code example is available for download here: http://abouttesting.fileave.com/DictionaryClass.vbs

When using QTP the debug view looks like this:

1 comment:

comprar tablet pc said...

Pretty helpful material, much thanks for this article.