Sunday 2 February 2014

Using Wrapper classes in QTP!!!!!!!!!!!!!!

One of my biggest complaints with VBScript is the lack of inheritance/interfaces. I know, I know...it's a scripting language and I am asking a lot. It wasn't my choice, however, to use this language as the one that drives QTP (and other testing tools). 

I hear some of you groaning about me preaching objects again, but I don't mind. You go on ahead an groan. :-)

My complaint stems from the fact that we cannot inherit and extend objects in VBScript like I would like to be able to do. As I've mentioned in prior posts, being able to create the myBrowser class from the QTP browser would rock. You can, as I've also previously noted, extend the interface of an object some using function libraries and registering functions to given objects. This works, but it's not as tidy as I'd like it to be. It also seems to be fragile somehow, mostly because it's not as encapsulated as I'd like it to be.

So, if you want to extend objects and don't like the user function idea (although I use it lot), how do you proceed?

Simulated inheritance. 

What is it? It's done using a wrapper in VBScript. If I use a wrapper, I can have as many objects wrapped up together as one cohesive unit and the user will never even know I've done it (probably). For example:

Let's assume I want to create a class that contains a lot of the functionality of 2 other objects, one of which has a name property and one of which has a number property. I could do this:

Class myNewClass

    Dim myParentObject1
    Dim myParentObject2

    Public Property Get Name()
       Name = myParentObject1.Name
    End Property

    Public Property Let Name(ByVal Value)
       myParentObject1.Name = Value
    End Property

    Public Property Get Number()
       Number = myParentObject1.Number
    End Property

    Public Property Let Name(ByVal Value)
       myParentObject1.Number = Value
    End Property

End Class

See? The caller of my class will only see the public interface for the wrapper:

Dim myObject

Set myObject = NewMyNewClass 'See previous post about objects on why this looks like does

myObject.Name = "Theo"
myObject.Number = 42

Additionally, I can write some code in the wrapper that might improve/change the functionality of the "base" class. A good example of this is a class I built recently that gives me the power of a Dictionary object's associative recall with the next/previous/movefirst/movelast functionality of DataSets. I can now either get the values out of the dictionary via a key or iterate it. Can I interate it using the Keys array off the object? Sure, but if I wrap this up, then it becomes simple and standard using a Do/Loop

Now, this is NOT inheritance. I may even be treading on toes using the term "simulated inheritance". Please don't tell anyone I said that VBScript supports inheritance. 

Where this really falls down, though, is when you want to build an object hierarchy. Imagine that your class contains a class that contains a class.....merely setting a property would be a royal pain. My advice? Keep the wrapper classes no more than one object level deep. Sure, use three objects on the same level, but avoid going any deeper.

Until QTP ships with a fully OOP language for scripting (like Rational Functional Tester does...it uses either .Net or Java), this is about the best we can do.

Sudhakar.Mangi

No comments:

Post a Comment