June 2, 2008

QTP and Tricks with Classes


As a hardcore WinRunner test automater, I still not like the "one line, one statement" approach of VBScript, but the one thing I like is working with classes. I'm still playing around with it a little bit and I learn a bit each day. The thing I did today isn't new I think, but for me it was.

A little about my situation. We are (ab)using QTP as a dataentry tool. We can only use the frontend, because the backend is protected. (so long for third party software). There will be a backend interface soon, but soon in the ICT is still counted in multiple months and a quicker solution had to be found. Maybe that is why QTP is chosen. On the contrary, I think the program manager felt for the 'Q'. Right now, the data is coming from multiple sources and will be directed to the frontend with QTP.

And this is where I discovered a neat application of classes for the use in dataentry. I created a class, let's say "customer" and I redirected the class into a function as a method of that class.
Time for an example:

First, my class 'customer':

class customer
    public name
    public address
    public phone
end class

and my function to enter the customer into the frontend:

public function enterCustomer(byRef myCus)    ' Use byRef for speed and to save memory

    call navigateToCustomerCreate()        'I like functions more then subs

    with Browser(zzz).Page(yyy)
        .WebEdit("Name").Set myCus.name
        .WebEdit("Address").Set myCus.address
        .WebEdit("Phone").Set myCus.phone
    end with

end function

now, I added a function into the customer class to enter the customer:

class customer
    public name
    public address
    public phone

    public function enterData()
        call enterCustomer(me)        'The 'me' is referring to it's own class object 'customer'
    end function
end class

This creates a method for my customer object to enter my data.

Because a customer can have multiple accounts, I created a class account and that one is added as a dynamic array in the customer class. And for data integrity I created a validate method that checks if all mandatory fields are filled and if all data is in the correct formatting (hurray for regular expressions).
But I can add as much functionality as I like in a ordered way, the only call I have to make to get it all working is using the enterData() method in my driver script for entering all my data into the frontend.

The main reason why I use this method, is that I only have to write an import function for all three types of import (plain text, xml and through an ODBC connection) and redirect the data into the class. At the end I call myCustomer.enterData() and it is done.

In the mean time, testing my functions is very easy, I just create an object of the correct class and test the function by calling enterCustomer(myCustomer). I don't have to bother validation rules because they are only executed when the enterData() method is called. And because the method validate() is a public method in the customer class, I do not have to enter the data when I want to test the validation of it.

It seems a lot of work for only adding a customer but in the real situation, the datamodel contains a lot more objects with a lot more fields. This way, it keeps my code clean, structured and also important: testable.

3 comments:

Anonymous said...

I like what you are doing with enterCustomer(me). That is a good way to reduce redundant code, and something I hadn't thought of. I'm going to give that a try next time I have a chance.

Anonymous said...

I am admittedly very much a newbie insofar as object oriented programming. I'm trying to understand your call to navigateToCustomerCreate().

I don't see where this sub/function is defined.

Wandering Guy said...

Hi,

A good post on Automated Chaos. We are VA4World a virtual assistant firm who provide admin support service for SME’s all over the world. We would like to hear your feedback.

Thanks,
Sridhar – VA4World for viral marketing, SEO and admin support