Geeks With Blogs

News QTP and Stuff
Theo Moore Questions... Morphology? Longevity? Incept dates?


First off, let me say that the guys over at The Software Inquisition  have written a very good article on the use of classes in QTP. I have been looking at the applications I will be automating at my new employer, and been considering using classes to do some of the heavy lifting for me. Whilst considering them, I had some thoughts, and thought I might expand on their example.

First off, VBScript classes don’t produce true objects. Why? Well, there is 1) no mechanism for inheritance, and 2) there is no polymorphism. In truth, the only thing they do provide is encapsulation. Now, that said, encapsulation is very powerful and it more than worth the time to properly create VBScript classes. Classes allow great reusability and allow disparate developers/SAE (Software Automation Engineers) to use them. So long as the original set of properties or methods is not altered by removal, any code based on them will continue to work. Additionally, the object can be written to be very smart, where it can compensate for all manner of problems like bad data, etc. Further, it provides a nice convenient package one can pass around cleanly unlike simply using functions. QTP Action code isn’t quite as trivial to use for jobs like these.

The article above explains what I consider the single greatest problem with using VBScript classes with QTP: there isn’t a simple way to instantiate your object. Since it’s not available via COM, you can’t simply use CreateObject to instantiate it. I’ve considered some alternatives to this (registering VBScript classes….yes it can be done or writing a COM DLL that that requires distribution and registration on every machine), but I think the approach demonstrated over at Software Inquisition is still the best. There is also one upside to this also: if you use a function call to return a fully instantiated object, you can force the call to some initialization code, such as I did in my example. The beauty of this is, of course, no surprises down the road….at least in theory.

I was trying to think of what I consider to be a good example of when such functionality would be handy, and honestly, I can think of tons of applications. Probably the single biggest one, though, is to get data from a database in a simple way. Imagine a single object with all of the code necessary to get or update data encapsulated such that only a few lines of code will produce results. Pretty darn handy, if you ask me.

At the above link, you will see a very simple example (but a great one) of a VBScript class. I was thinking something a little more advanced might be in order. I have some code here that basically shows a quick and easy class based on a dictionary object (from filescriptingobject, of course) that encapsulates all the code necessary to create what basically amounts to a collection of objects. The objects can come from anywhere, and the class can contain the code to handle all of it; your user need not know anything about how it works:

Option Explicit

Class EClass
    'This is a sample class showing how a dictionary can be used to contain a collection 'of classes

    Private dInternal

    Public Sub Init()
        Set dInternal = CreateObject("Scripting.Dictionary")
    End Sub


    Public Sub LoadRecords(vCriteria)
        'The beauty here is that you could use whatever you want,
        'and pass whatever you want in. It could be a sql statement,
        'an id or whatever. The code to load the objects could be
        'sophisticated to find whatever you want.
        'In this case, I am simply going to create some items and stick them
        'In the dictionary. I am ignoring the parameter at the moment

        Dim lngCount

        For lngCount = 1 to 5
                 Dim NewPerson

                 Set NewPerson = New Person

                With NewPerson
                    .FirstName = "Theo" & CStr(lngCount)
                .LastName = "Moore" & CStr(lngCount)
                .TestNumber = "C" & CStr(lngCount)
            End With

            dInternal.Add CStr(NewPerson.TestNumber), NewPerson

            Set NewPerson = Nothing
        Next 'lngCount

    End Sub


    Public Function GetItem(vKey)
        Dim vItem

        Set vItem = Nothing
        'Doing some error handling here since I am going
        'to check the object myself in a minute
        On Error Resume Next
        Set vItem = dInternal.Item(vKey)
        On Error Goto 0

        Set GetItem = vItem
    End Function

    Public Function ItemCount()
        ItemCount = dInternal.Count
    End Function

End Class

Public Function NewEClass()
    'This function call allows me to create the object and pass it out
    Set NewEClass = New EClass
End Function

Class Person

    Private mfirstName
    Private mlastName
    Private mtestNumber

    Public Sub Init()
        'do some stuff
        'Set intial values for variables
        mfirstName = "Empty"
        mlastName = "Empty"
        mtestNumber = 0
    End Sub

    Public Property Let FirstName(vNewValue)
        mfirstName = vNewValue
    End Property

    Public Property Get FirstName()
        FirstName = mfirstName
    End Property

    Public Property Let LastName(vNewValue)
        mlastName = vNewValue
    End Property

    Public Property Get LastName()
        LastName = mlastname
    End Property

    Public Property Get TestNumber()
        TestNumber = mtestNumber
    End Property

    Public Property Let TestNumber(vNewValue)
        mtestNumber = vNewValue
    End Property

End Class

Public Function NewPerson
    Set NewPerson = new Person
End Function

Now, it’s worth noting here that the VBScript means to get at the new classes is a bit awkward, but it does work. It’s also interesting to note that I have been pulling my hair out because I keep forgetting how much the syntax changes between VBScript and VB .Net. I keep forgetting things like the SET keyword…..

Anyway, here’s some very simple code that you could place in a QTP action (with the above classes somewhere in scope) to get the collection of your defined objects back. Note that I’ve implemented a Count property and a way to get at specific items. Your user could use such an object and never need know how it works:

Option Explicit

Dim cCollectionTest
Dim TestItem

Set cCollectionTest = NewEClass()

'Remember that I am not currently using the parameter, so I am sending nothing
'If I was actually using this, I might send an ID or something...

Set TestItem = cCollectionTest.GetItem("C2")

With TestItem
    msgbox .FirstName & " " & .LastName
End with

msgbox cCollectionTest.ItemCount()

Posted on Saturday, December 29, 2007 11:11 PM | Back to top

Comments on this post: QTP: Using VBScript Classes

# re: QTP: Using VBScript Classes
Requesting Gravatar...
i need sample and simple scripts to learn QTP.
Left by girish nayakar on Jan 23, 2008 1:23 AM

# re: QTP: Using VBScript Classes
Requesting Gravatar...
I'm glad to see you blogging about QTP again. This is an insightful post as always. I hope that Delphi doesn't pull you too far in the direction of WinRunner and completely away from QTP.
Left by Will on Jan 31, 2008 1:48 PM

# re: QTP: Using VBScript Classes
Requesting Gravatar...
hi ,
ineed to learn vb scripts for QTP
Left by sirisha on Feb 05, 2008 6:27 AM

# re: QTP: Using VBScript Classes
Requesting Gravatar...
Need to learn Vbscripts programming
Left by jenny on Feb 15, 2008 12:57 AM

# re: QTP: Using VBScript Classes
Requesting Gravatar...
I want to learn VB script for QTP.
Left by Zaka Ulla on Feb 19, 2008 1:41 AM

# re: QTP: Using VBScript Classes
Requesting Gravatar...
very useful.. great. thanks a lot.
Left by forum on Apr 10, 2008 6:38 AM

Comments have been closed on this topic.
Copyright © Theo Moore | Powered by: