Login   /   Register

Class inheritance in VBScript – Now at your doorstep!

Rate this article
     6 votes, average: 4 out of 56 votes, average: 4 out of 56 votes, average: 4 out of 56 votes, average: 4 out of 56 votes, average: 4 out of 5
Loading ... Loading ...
October 4th, 2008 by Yaron Assa

Google Search Results

You arrived here after searching for the following phrases:

Click a phrase to jump to the first occurrence, or return to the search results.

ChangeLog:

5/10 : I’ve uploaded a new file with the fixed parser. Added support for code blocks with sensitive names (Function PrivateFunciton, Sub SpecialGet etc.)

10/10 : I’ve uploaded a new file with constructor arguments support, and a new regular expression engine (preserves comments and should make things go faster).

Notice that the new file requires a new run command (use NewRegExParser.dll instead of ClassParser.Dll).

To use constructor arguments:

1. In your Class_Initialize sub, add parameters with a comment. For example: Sub Class_Initialize ‘(Param1, Param2)

2. When you create a new instance of your class, use comments to deliver values to these parameters. For example Set x = new MyClass’(5, “Something”)

You can pass parameters and variables. However, these will always be passed as ByVal.

 

The problem

One of QTP’s biggest downsides is its VBScript engine. While it may answer most of your everyday needs, it falls short when it comes to classes and object-oriented programming.

On the top of my personal VBScript missing features list is the ability to perform class inheritance, which could save tons of duplicate code, and significantly reduce maintenance overheads. Here’s a quick example for class inheritance. Imagine this is a basic “engine” template:

Class AbstractEngine
    'This will include general methods, variable and properties
 
    Private pEngine    'Each engine will hold its object here
    Private pFileName    'The location for the output
 
    Public Property Get File
        File = pFileName 'This is the same for all engines
    End Property
 
    Public Sub StartEngine
        'Each engine should implement this on its own
    End Sub
 
    Public Sub Report(sEventName)
        'This is the same for all engines
        pEngine.Report sEventName
    End Sub
 
End Class

We would like to write specific engines that would implement this more general class template. In every other language, we could’ve inherited the AbstractEngine, and immediately reuse its code without having to rewrite it specifically in our own “derived” class. This would allow us to maintain the “base” code in only one place – the abstract class, and all the derived class would immediately inherit our repairs and changes.

However, as I’ve noticed, VBScript does not support class inheritance, thus forcing us to manually copy the base code to all our derived classes. This means we would have to duplicate changes in multiple files, projects and computers – severally hampering our automation ROI.

The solution

The content you are accessing is for registered users only
Registeration is FREE, quick and private.
You can either Register pr Login if you have already registered

Summery

VBScript’s lack of class inheritance can be quite problematic for those of us who are using Classes regularly throughout our scripts. The attached file provides a parser which enables you to effectively use class inheritance in VBScript by placeing ‘<Inherit> ParentClassName comments within your classes. The mechanism will:

1. Enable inheritance from multiple parents (just use multiple lines with Inherit comments)

2. Copies Properties, Variables, Functions and methods from the parent class to the derived class.

3. In case the derived class already has a code segment with the same name, the copied code-segment is changed to MyBase_<Name>. Inner segment placements are updated accordingly.

4. Enables inheritance through multiple hierarchies (e.g. GrandParent->Parent->Child inheritance). Circular inheritance protection. The order of appearance in the files is irrelevant, the parser will make sure the parent classes are parsed before their derived classes.

5. Enables passing arguments to Class_Initialize constructors by using structured comments.

6. The parser has a major downside – as the technique uses ExecuteGlobal to load the processed classes into QTP, you cannot debug the code in mid-run.

Reliability

I’ve tested the parser on a wide variety of code libraries, and it seems to be working fine. Having said that, there’re probably some undocumented bugs which might prove to be catastrophic – be warned.

I’ve not planing on actively maintaining the parser, but if you’ll report bugs that are easily fixed, I’’ll work on them and post repaired versions when I get the chance. The parser is distributed AS IS, with NO WARANTIES, and should be regarded as a Beta / Alpha release.

Future Releases

This is just a preliminary work on the way to an integrated QTP parser. The end goal is being completely transparent for the user. Pressing F5 will run the test as usual, and the parser will work in the background in a manner which will allow debugging though the run-session.

I already have archived some milestone toward this goal – If you’d like to participate in the product’s beta group, please contact me and say so. Not everyone will be able to participate – so don’t get your hopes up :) .

Posted in QTP Hacks, Using Classes, VBScript Techniques

13 Responses to “Class inheritance in VBScript – Now at your doorstep!”

  1. wroden Says:

    [+]

    I'm downloading this immediately. What a simple and elegant answer to a major shortcoming. As for debugging; if there were a met... ...

  2. Stefan Thelenius Says:

    [-]

    This feature looks very promising…

    Excellent work!

  3. anshooarora Says:

    [-]

    Great solution Yaron. Excellent solution, indeed.

  4. Yaron Assa Says:

    [+]

    Thanks, One major bug I've found is the inability to parse code segmnets with names the contain Sub, Function, Class etc. I'... ...

  5. shiv_nsk Says:

    [+]

    Great Solution! Could you please tell me how it will resolve when we try to do multiple inheritance and when the two base classe... ...

  6. Yaron Assa Says:

    [+]

    Hey shiv_nsk The algorithm for conflicting code segments is as follows: 1. If no conflict occurs, the derived class copies the se... ...

  7. erichneu Says:

    [+]

    I've great expectations and I'm keen to see the Future Release including debugging facilities. Great work so far. regards - Er... ...

  8. heqingbluesky Says:

    [-]

    Without enough support of Class usage is the most disappointing pitfall you have to tackle.

  9. glennh Says:

    [+]

    I'm concerned about the debug angle. My experience has been that automation developers often do not give enough consideration to ... ...

  10. johnjohn2 Says:

    [+]

    Hi all, I've created a different solution to this problem which I posted over at 4guys: http://www.aspmessageboard.com/showthrea... ...

  11. Yaron Assa Says:

    [+]

    Reiss - Interesting solution - I wasn't aware you could do that with GetRef. It's pretty cool! glennh - I understand your thought... ...

  12. Reiss Says:

    [+]

    Hi all, I've updated the technique in the above post to provide better performance and super easy syntax: http://www.aspmessageb... ...

  13. Yaron Assa Says:

    [-]

    Thanks a lot for sharing your code!

Leave a Reply

You must be logged in to post a comment.

This article was viewed 1433 times