June 1, 2008
QTP and Four Ways of creating Classes
QTP isn't build for the use of classes. If it was, we had a separated file where we could store our classes, a viewer extension and a set of exotic functions with almost enough functionality to handle our custom classes.
But we haven't have any of them, so we have to fiddle around with methods Mercury (I refuse to call them HP until their service is to the level we are used to experience) thought we are not capable of to use: 'Coz we're testers - not programmers.
These are four methods to implement classes. I found the last three on the net, the source is just down each method.
1. Just put them in your action script
And enjoy the experience of non-reusable classes. Just the main benefit of a class: reusability is taken away from you, because with each new action script, you have to define your class again (if you want to use it of course). This will only suit you if you only have one action script.
This is the case on my current project, because we don't use Quality Center and we use the action script more as the main driver then as what it is meant to be.
I still rather don't want to use it in the way I just described, because with only ten classes in our data model it is still getting crowded in the action script.
2. Put them in a function script
You can put your class into a separate function script and just add it to your project. But QTP wouldn't be QTP if this worked on first glance. As long as you only put it in there as a class, it wouldn't work, because in some way the class is seen as a private on that function library. It will not be recognized in other parts of your script.
The work around:
First, make a class
class prototypeContact
public name
public address
public phone
end class
and create a function (in the same library), setting an object as that class and returning that object:
public function Contact()
set Contact = new prototypeContact
end function
Now you can use your class by calling the function with a newly created object:
set myContact = Contact()
The function Contact() will assign a new object to myContact of the class prototypeContact.
First found on The Software Inquisition.
3. Put them in an external script
Create your classes in a new script and save it as .vbs file. From your main script (or action script), do a call to this library with ExecuteFile().
This works with one major drawback, you'll have a hard time debugging. As soon an error is raised, it will mention the line of your main script were ExecuteFile() was called and that's it.
First found on a comment on an article of The Software Inguisition.
4. Load them dynamically
This is the most complex way, but also the one with the most potential. I will not go into the deepest detail, you can read that in the original article, but I will explain the principle:
1. Dynamically create a text string that contains the class definition.
2. Execute the text string with the command "Execute" (for the WinRunner and Javascript readers: It does the same as the eval() function).
3. Now you can set an object as the just created class.
Because you create the string dynamically, you can adapt the class on the fly. I wouldn't dare to talk about inheritance or polymorphism, but you can imagine the power of runtime named classes, variables, objects, functions and properties.
There is still the drawback of item 3: It gives you a hard time debugging it when something went wrong. And that is a very big drawback if you think about bugs that can enter dynamic written code.
First found in an article on the blog of Stefan Thelenius.
Final Word
Not all methods are working on each release of QTP. ExecuteFile() for example is available as of version 9.0. You'll have to find out what method suits you best.
Labels:
classes,
dynamic class creation,
QTP
Subscribe to:
Post Comments (Atom)
1 comment:
thanks. it was very useful..
Post a Comment