Sunday, January 11, 2009

How to use Classes in QTP

Here i am giving an useful information about how to use classes in QTP.

Before going to that we will discuss about using functions in QTP.

If there is a function in our script like this

'*********** ********* ********* ********* ********* ********* **
Function Demo(a,b)
demo=a+b
End Function
'*********** ********* ********* ********* ********* ********* **

This is a function to add two numbers.
To call this function we write
'*********** ********* *****
val=demo(2,3)
msgbox val
'*********** ********* *****
We can store the functions in library files (.vbs) and to use this function, Library should be associated to the QTP Test or library file should be executed by QTP Test using ExecuteFile statement.
This means we can use the functions which are written in QTP Script or in Library files by associating it or by executing it.

But this is not possible when using classes in our QTP Test. If we want to use a class which is there in the libraryfile its mandotory that the library should be executed using Executefile statement.
Classes will not work which are there in associated libraries. Here is an example...

'*********** ********* ********* ********* ********* **
Class Maths
Function Add1(a,b)
add1=a+b
End Function

Function sub1(a,b)
sub1=a-b
End Function

End Class
'*********** ********* ********* ********* ********* **
If the above code is there with in the test script then the usage of this class will be like this
'*********** ********* ********* ********* ********* **
Set mat=New Maths
val=mat.add1( 2,3)
msgbox val
'*********** ********* ********* ********* ********* **
If the class code is there with in the library file then the usage of this class will be like this
'*********** ********* ********* ********* ********* **
ExecuteFile (Library Path)
Set mat=New Maths
val=mat.add1( 2,3)
msgbox val
'*********** ********* ********* ********* ********* **
This class will not work If you just associate the library with in the
File--> Settings --> Resources
But there is a way of using the classes even though if it is in associated library.

First Method:
Where ever the class is declared in the library it self create the class instance and use it in your qtp script.
'*********** ********* ********* ********* ********* **
Set mat=New Maths

Class Maths
Function Add1(a,b)
add1=a+b
End Function

Function sub1(a,b)
sub1=a-b
End Function

End Class
'*********** ********* ********* ********* ********* **
To Use this class
'*********** ********* *
val=mat.add1( 2,3)
msgbox val
'*********** ********* *

Second Method:
Create a function which is returning class object by creating instance of the class.
'*********** ********* ********* ********* ********* **
Function mat()
Set mat=New Maths
End Function

Class Maths
Function Add1(a,b)
add1=a+b
End Function

Function sub1(a,b)
sub1=a-b
End Function

End Class
'*********** ********* ********* ********* ********* **
To Use this class
'*********** ********* *
Set mth=mat ' mat is a function which will return the class instance
val=mth.add1( 2,3)
msgbox val
'*********** ********* *

19 comments:

Sergey Pashuk said...

Great.It is easy and powerful!
Thanks, now I have basic view about classes in QTP.

Ajay Thirumalesh said...

Thanks.very useful information.
Ajay,
software testing engineer,
bangalore.

Anonymous said...

Thanks Man, it was useful.

Unknown said...

Thank for sharing, it's greate and useful.

Anonymous said...

Nice an Easy

Anonymous said...

Great and easy-to-understand tutorial.

I would like to know about the specific/particular usage of these class objects.

I don't know in which situations this would be very very useful.
Any advice.

Regards,
Suresh

Anonymous said...

Can you tell us which way is better on performance ? or does classes actually increase performance?

Shalabh Dixit said...

why the list of functions are not visible when we create a class object and tries to access it via object name and put dot(.) list of all the functions should be available... but its not like that here.... :(

we need to manually type the function name

Anonymous said...

The code written needs some chnages as the functions under class are not returning any value how r u priting this msgbox

you should add one line here
add1 =

Harsha Vardhan Reddy said...

Goog One Boss..
thank You

Harsha Vardhan Reddy said...

Good one Boss..
Thank You

Anonymous said...

Excellent!
Very easy to understand.

Though I have been seeing the classes in vbs, till now I could not understand them.

sach said...

Hi i am very new into testing currently i am working in one organisation as Quality Assurance, i want to learn QTP through online, i got many ebooks but i am not satisfy with that if you have something related QTP Basics then just mail me to kakadesachin.kakade@gmail.com
Please send links regarding QTP online Training and ebooks also .I am writing this comment to other user also if they have such info.Or if you have any soft copy in your own words will be very helpful for me. Because i am not getting much time to join QTP course in institutes
Thanx
Sachin

Unknown said...

i am new learner,its easy understandable manner and its help me a lot. please send this tutorials to my mail...................
my mail id : Sandyaddl@gmail.com

Unknown said...

thanks...
I am a new learner of QTP.this tutorials are help me a lot .
please send this tutorials my mail
my mail id Sandyaddl@gmail.com

Anonymous said...

thanks, nice post. helped to understand class

Anonymous said...

Thanks for posting...very helpful information

Gautam Grover said...

thank u for nice concept

Anonymous said...

nice post...

About Me

My photo
Hi all,I am Arun Singh Rajaputra. I am working as a Project Lead in a reputed organization in Hyderabad, AP, India. I have little bit knowledge on Quick Test Professional and like to share my knowledge to all through this blog.

Subscribe to get updates on this blog

Enter your email address:

NOTE

Some of the posts posted in this blog are collected and most of them have been prepared by me. If any one have objections regarding any copied posts. Please mail me so that I can take a necessary action on that post.